Laravel:在删除用户之前提示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26829734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:24:11  来源:igfitidea点击:

Laravel : Prompt before deleting a user

laravellaravel-4blade

提问by fanbondi

I am successfully deleting records in my table using the code below. My only problem, I want it to prompt the user to confirm the action before deleting.

我使用下面的代码成功删除了我的表中的记录。我唯一的问题是,我希望它在删除之前提示用户确认操作。

{{link_to_route('individualprofiles.edit', 'Edit', array($ip->id))}}
{{Form::open(array( 'route' => array( 'individualprofiles.destroy', $ip->id ), 'method' => 'delete', 'style' => 'display:inline'))}}
     {{Form::submit('D', array('class' => 'btn btn-danger'))}}
{{Form::close()}}

回答by The Alpha

You may try something like this:

你可以尝试这样的事情:

STEP – 1

第1步

First of all, you need to add bootstrap.cssfile using a link tag, jQueryand bootstrap.jsfile using a script tag in your head section of your web page. On bootstrap's website, you would find details about this. It's also possible to build a customized file with selected components from here if you don't want to use full bootstrap framework.

首先,您需要bootstrap.css使用链接标签添加文件,jQuerybootstrap.js在网页的 head 部分使用脚本标签添加文件。在 bootstrap 的网站上,您可以找到有关此内容的详细信息。如果您不想使用完整的引导程序框架,也可以使用此处选择的组件构建自定义文件。

STEP – 2

第2步

Add following html code in a file and save it as a separate file, for example delete_confirm.php

将以下html代码添加到文件中,并将其另存为单独的文件,例如 delete_confirm.php

<!-- Modal Dialog -->
<div class="modal fade" id="confirmDelete" role="dialog" aria-labelledby="confirmDeleteLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title">Delete Parmanently</h4>
      </div>
      <div class="modal-body">
        <p>Are you sure about this ?</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
        <button type="button" class="btn btn-danger" id="confirm">Delete</button>
      </div>
    </div>
  </div>
</div>

Also, paste following JavaScript code in the same file as well

此外,将以下 JavaScript 代码也粘贴到同一文件中

<!-- Dialog show event handler -->
  $('#confirmDelete').on('show.bs.modal', function (e) {
      $message = $(e.relatedTarget).attr('data-message');
      $(this).find('.modal-body p').text($message);
      $title = $(e.relatedTarget).attr('data-title');
      $(this).find('.modal-title').text($title);

      // Pass form reference to modal for submission on yes/ok
      var form = $(e.relatedTarget).closest('form');
      $(this).find('.modal-footer #confirm').data('form', form);
  });

  <!-- Form confirm (yes/ok) handler, submits form -->
  $('#confirmDelete').find('.modal-footer #confirm').on('click', function(){
      $(this).data('form').submit();
  });

STEP – 3

步骤 – 3

Now, in any page where you want to use this confirm modal dialog, just include it using require_onceor include_oncelike:

现在,在您想要使用此确认模式对话框的任何页面中,只需使用require_onceinclude_once类似的方式包含它:

// other code
require_once('delete_confirm.php');

STEP – 4

第四步

To build a delete action button you may use something like this:

要构建删除操作按钮,您可以使用以下内容:

<form method="POST" action="http://example.com/admin/user/delete/12" accept-charset="UTF-8" style="display:inline">
    <button class="btn btn-xs btn-danger" type="button" data-toggle="modal" data-target="#confirmDelete" data-title="Delete User" data-message="Are you sure you want to delete this user ?">
        <i class="glyphicon glyphicon-trash"></i> Delete
    </button>
</form>

For more information check this article

有关更多信息,请查看这篇文章

回答by Mukesh Chapagain

By using 'onsubmit' => "return confirm('Are you sure you want to delete?')"inside Form::open.

通过使用'onsubmit' => "return confirm('Are you sure you want to delete?')"内部Form::open.

{{Form::open(array( 
    'route' => array( 'individualprofiles.destroy', $ip->id ), 
    'method' => 'delete', 
    'style' => 'display:inline',
    'onsubmit' => "return confirm('Are you sure you want to delete?')",
))}}
     {{Form::submit('D', array('class' => 'btn btn-danger'))}}
{{Form::close()}}

回答by Laravelian

Add an ID or class to your form, and use javascript to catch the forms submit action. Prompt the user, and if they confirm, use javascript to submit the form.

向您的表单添加一个 ID 或类,并使用 javascript 来捕获表单提交操作。提示用户,如果他们确认,使用 javascript 提交表单。

Another way to do this without javascript is to create a view that prompts the user to confirm the delete. Your delete button becomes a link to the confirm page, and the confirm page would contain this form.

另一种不使用 javascript 的方法是创建一个视图,提示用户确认删除。您的删除按钮变成了确认页面的链接,并且确认页面将包含此表单。