【问题标题】:Delete confirmation with sweet alert 2使用甜蜜警报 2 删除确认
【发布时间】:2019-03-26 15:39:20
【问题描述】:

我想用 php 删除一条数据库记录。在我提交表单之前,我想要一个带有“是或否”按钮的 Sweet Alert (2) 通知。

当“是”时,我想提交表单并激活我的 php 代码。

问题是它不起作用。单击“是”后,我的页面正在刷新,并且我的 php 代码无法启动。

<form action="" method="POST">
    <div class="right gap-items-2">
        <button class="btn btn-error" name="archive" type="submit" onclick="archiveFunction()">archive</button>
    </div>
</form>


<script>
    function archiveFunction() {
        event.preventDefault(); // prevent form submit
        var form = event.target.form; // storing the form
        Swal.fire({
            title: 'Are you sure?',
            text: "You won't be able to revert this!",
            type: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: 'Yes, delete it!'
        }).then((result) => {
            if (result.value) {
                form.submit();
            }
        })
    }
</script>


if (isset($_POST['archive'])){

$message = "test";
echo "<script type='text/javascript'>alert('$message');</script>";

}

【问题讨论】:

    标签: javascript php jquery sweetalert2


    【解决方案1】:

    使用这样的 ID 修改表单按钮:

    <form action="" method="POST">
        <div class="right gap-items-2">
            <button class="btn btn-error" name="archive" type="submit" id="submitForm" >archive</button>
        </div>
    </form>
    

    然后是这样的脚本:

    <script>
        $('#submitForm').on('click',function(e){
            e.preventDefault();
            var form = $(this).parents('form');
            Swal.fire({
                title: 'Are you sure?',
                text: "You won't be able to revert this!",
                type: 'warning',
                showCancelButton: true,
                confirmButtonColor: '#3085d6',
                cancelButtonColor: '#d33',
                confirmButtonText: 'Yes, delete it!'
            }).then((result) => {
                if (result.value) {
    
                    form.submit();
                }
            });
        });
    </script>
    

    【讨论】:

    • 我应用了它,但不幸的是它不起作用。他不想接触我的 PHP。
    • 目标php文件路径是什么?如果你没有指定地址,它会将表单数据发送到当前地址
    【解决方案2】:
    ##php/file:##
         <?php
                    $no = 1;
                    $sql = "SELECT * FROM office_info";
                    $query = $conn->query($sql);
                    while ($rows = $query->fetch_assoc()) {
                        echo
                            "<tr>
                                        <td>" . $rows['id'] . "</td>
                                        <td>" . $rows['office_name'] . "</td>
                                        <td>" . $rows['office_type'] . "</td>
                                        <td>" . $rows['office_address'] . "</td>
                                        <td>" . $rows['office_cont_person'] . "</td>
                                        <td>" . $rows['office_con_mobile_no'] . "</td>
                                        <td>" . $rows['office_start_dt'] . "</td>
                                        <td>" . $rows['office_end_dt'] . "</td>
                                        <td>
                                        <a href='office_info_edit.php?id=" . $rows['id'] . "' target='_blank' class='btn btn-success btn-sm><span class='glyphicon glyphicon-edit'></span>Edit</a>
                                        <a href='office_info_delete.php?id=" . $rows['id'] . "' target='_blank' class='btn btn-success btn-sm><span class='glyphicon glyphicon-edit'></span>del</a>
                                        <a data-id='" . $rows['id'] . "' id='delete_id' class='btn btn-danger btn-sm'<span class='glyphicon glyphicon-trash' href='javascript:void(0)'></span>delete</a>
                                    </td>
                                    </tr>";
                    }
                    ?>
    ##script##
        <script>
           $(document).ready(function() {
                $(document).on('click', '#delete_id', function(e) {
                    var id = $(this).data('id');
                    SwalDelete(id);
                    e.preventDefault();
                });
            });
    
            function SwalDelete(id) {
                Swal.fire({
                    title: 'Are you sure?',
                    text: "It will be deleted permanently!",
                    type: 'warning',
                    showCancelButton: true,
                    confirmButtonColor: '#3085d6',
                    cancelButtonColor: '#d33',
                    confirmButtonText: 'Yes, delete it!',
                    showLoaderOnConfirm: true,
                    preConfirm: function() {
                        return new Promise(function(resolve) {
                            $.ajax({
                                    url: 'office_info_delete.php',
                                    type: 'POST',
                                    data: 'id=' + id,
                                    dataType: 'json'
                                })
                                .done(function(response) {
                                    Swal.fire('Deleted!', 'Your file has been deleted.', 'success')
                                })
                                .fail(function() {
                                    Swal.fire('Oops...', 'Something went wrong with ajax !', 'error')
                                });
                        });
                    },
                });
            }
        </script>
        **office_info_delete.php**
        if (isset($_POST['id'])) {
            require '../database.php';
            $id = intval($_POST['id']);
            $deleteQuery = "delete from `office_info` where id='$id' limit 1";
            $conn->query($deleteQuery);
            if ($conn->affected_rows == 1) {
                $response['status']  = 'success';
                $response['message'] = 'Product Deleted Successfully ...';
            } else {
                $response['status']  = 'error';
                $response['message'] = 'Unable to delete product ...';
            }
            echo json_encode($response);
        }
    

    【讨论】:

    • 请考虑editing 您的答案以添加一些解释和详细信息。虽然它可能会回答问题,但仅添加一些代码并不能帮助 OP 或未来的社区成员理解问题或解决方案。
    • 是的,我会添加详细信息
    【解决方案3】:

    $('#deleteamc').on('click',function(e) {
        
        event.preventDefault();
    var form = this;
        
            swal({
      title: "Are you sure?",
      text: "All data related to this AMC ID will be parmanently deleted",
      type: "warning",
      showCancelButton: true,
      confirmButtonColor: "#DD6B55",
      confirmButtonText: "Yes, DELETE it!",
      cancelButtonText: "No, cancel please!",
      closeOnConfirm: false,
      closeOnCancel: false
    },
    function(isConfirm){
      if (isConfirm) {
    	  form.submit();
    	  } else {
        swal("Cancelled", "AMC Record is safe :)", "error");
    	
      }
    });
    });
    <link href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.css" rel="stylesheet"/>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    
    <form method="POST" id="deleteform">
            <input type="hidden" name="amccode" id="amccode" value="<?php echo $row['amccode']; ?>">
            <button class="btn btn-danger" id="deleteamc" name="delete" type="submit">
                <i class="fa fa-minus"></i>
                    Delete
            </button>
    </form>
    <!-- USER CONFIRM DELETE THEN FIRE DELETE FUNCTION -->
    
    <?php
    
    if(isset($_REQUEST['delete']))
    		 {
    			$amccode=$_POST['amccode'];
    			
    	 
    		 echo "WRITE DELETE SQL CODE";
    		 exit;
    	
    					
    			
    			");
    ?>
    			 
    			 <script src="../source/alert/dist/sweetalert.min.js"></script>
    <link rel="stylesheet" type="text/css" href="../source/alert/dist/sweetalert.css">
    			 
    <script type="text/javascript">
    function SuccessSaved (){
    	
            swal({ 
                  title: "Success",
                  text:  "All Records Successfully Deleted. Customer ID <?php echo $amccode ?>",
                  type: "success",
    			  
            },
                 function(){
                   
    			window.location.href = "<?php echo BASE_URL; ?>/admin/amcall.php";
            });
    }
    
    </script>
            <script type="text/javascript" >
            $( document ).ready(function() {
    			//console.log( "ready!" );
    			SuccessSaved();
    		});
    		
    		
    </script>
    
    		<?php	 
    		 }
    
    
    ?>		

    【讨论】:

      【解决方案4】:

      试试这个,我在 laravel 里用过这个,效果很好

      <td>
                                                      <form class="archiveItem"
                                                            action="{{route('archive.destroy',['id'=>$archive->id])}}"
                                                            method="Post">
                                                          {{method_field('DELETE')}}
                                                          {{csrf_field()}}
      
                                                          <input type="hidden" name="archive">
                                                          <a onclick="archiveRemove(this)" class="deleteRecord"
                                                             id="{{$archive->id}}"
                                                             style="cursor: pointer"><i class="fa fa-trash"
                                                                                        style="font-size: 20px;color: #000"></i>
                                                          </a>
                                                      </form>
      

      在脚本中:

      function archiveRemove(any) {
              var click = $(any);
              var id = click.attr("id");
              swal.fire({
                  title: 'Are you sure !',
                     text: "?????",
                     type: 'warning',
                     showCancelButton: true,
                     confirmButtonColor: '#3085d6',
                     cancelButtonColor: '#d33',
                     confirmButtonText: 'yes!',
                     cancelButtonText: 'no'
              }).then(function (success) {
                  if (success) {
                      $('a[id="' + id + '"]').parents(".archiveItem").submit();
                  }
              })
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-04-03
        • 2015-09-17
        • 2017-01-04
        • 1970-01-01
        • 2017-07-16
        • 2023-03-08
        • 2017-08-30
        • 1970-01-01
        相关资源
        最近更新 更多