简体   繁体   English

甜蜜警报确认后表单未提交

[英]Form is not submiting after the sweet alert confirmation

I'm having a table which contains some rows of data and I have delete button for each row.我有一个包含一些数据行的表,并且每行都有删除按钮。

My form is not submiting if I implement sweetalert2 with my code, what I need is, I need to delete my row only after sweet alert confirmation button.如果我用我的代码实现 sweetalert2,我的表单不会提交,我需要的是,我只需要在 sweet alert 确认按钮后删除我的行。 Here is my Code;这是我的代码;

 <tbody> <?php foreach ($category_details as $category_detail): ?> <tr> <td>...</td> <?-- etc --> <form method="post"> <td> <input type="hidden" name="delete-id" value="<;php echo $category_detail['id']? ?>"> <button type="submit" name="single-cdelete" class="swa-confirm btn btn-trash-alt"> <i class="fas fa-trash-alt"></i> </button> </td> </tr> <?php endforeach ?> </tbody> </form>

 if(isset($_POST['single-cdelete'])){ $delete_id = $_POST['delete-id']; $delete_image = $_POST['delete-image']; category_delete($delete_id, $delete_image); } function category_delete($delete_id, $delete_image){ global $db; if(mysqli_query($db, "DELETE FROM categories WHERE id =$delete_id")){ unlink('../assets/images/categories/'.$delete_image); $_SESSION['success'] = "Category has been deleted successfully"; }else{ $_SESSION['success'] ="Something went wrong, Try again"; } }

My SweetAlert Code:我的 SweetAlert 代码:

 <script> $(".swa-confirm").on("click", function(e) { e.preventDefault(); Swal.fire({ title: "Are you Sure?", text:"You want to Delete the selected Category", type: "warning", showCancelButton: true, confirmButtonColor: "#cc3f44", confirmButtonText: "Delete", closeOnConfirm: true, html: false }, function( confirmed ) { if( confirmed ){ $('.swa-confirm').submit(); } }); }); </script>

As it's evident from your code, you've used jQuery to perform a form submission.从您的代码中可以明显看出,您已使用 jQuery 执行表单提交。

As per the documentation, https://api.jquery.com/submit/ , the method .submit() can only be used on a form .根据文档https://api.jquery.com/submit/ ,方法.submit() can only be used on a form

The submit event is sent to an element when the user is attempting to submit a form .当用户试图提交表单时,提交事件被发送到一个元素。 It can only be attached to <form> elements ..只能附加到<form>元素..

Forms can be submitted either by clicking an explicit, , or, or by pressing Enter when certain form elements have focus... Forms 可以通过单击显式、 或 或在某些表单元素具有焦点时按 Enter 来提交...

It can be seen that you're also relying on some other functionality Swal.fire(..) because of which, you had to do an event.preventDefault() , otherwise, the form would have submitted itself without hassle.可以看出,您还依赖于Swal.fire(..)的其他一些功能,因此,您必须执行event.preventDefault() ,否则,表单会毫不费力地自行提交。 But it's understandable that you will need this part of functionality.但是您需要这部分功能是可以理解的。

So, to solve your problem, you need to add some kind of identifier to your form eg a class or an id .因此,要解决您的问题,您需要在表单中添加某种标识符,例如classid So, instead of doing this:所以,与其这样做:

<form method="post">

..do, something like ..做,像

<form method="post" id="myform">

..and in the code snippet, use this identifier to call submit() : ..并在代码片段中,使用此标识符调用submit()

update : Also, notice that sweetalert2 supports a promise , and therefore, will recommend using a then -able promise and use a catch block to track any errors.更新:另外,请注意sweetalert2支持promise ,因此建议使用then -able promise 并使用catch块来跟踪任何错误。

$(".swa-confirm").on("click", function(e) {

  e.preventDefault();

  Swal.fire({
      ...
    }).then((confirmed) => {
      if (confirmed) {
        $('#myform').submit(); // << here
      }
    })
    .catch((error) => {
      console.log(error)
    });
});

as the comment from @Tangoabc Delta you just can use.submit() event on a form, so:正如@Tangoabc Delta的评论,您可以在表单上使用 .submit() 事件,因此:

  • first, give your form an id:首先,给你的表单一个ID:

     <form method="post" id="swaForm">
  • then use the script like this:然后像这样使用脚本:

     <script> $(".swa-confirm").on("click", function(e) { e.preventDefault(); Swal.fire({ title: "Are you Sure?", text:"You want to Delete the selected Category", type: "warning", showCancelButton: true, confirmButtonColor: "#cc3f44", confirmButtonText: "Delete", closeOnConfirm: true, html: false }).then(function() { $('#swaForm').submit(); }) }); </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM