删除确认并显示甜蜜提示2

时间:2019-03-26 15:39:20

标签: javascript php jquery sweetalert2

我想用php删除数据库记录。在提交表格摘要之前,我希望通过“是”按钮通知“甜蜜提醒”(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>";

}

4 个答案:

答案 0 :(得分:1)

##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);
    }

答案 1 :(得分:0)

使用如下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>

答案 2 :(得分:0)

    public class test {
     public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
    if(sc.equals(""))
    {
        sc.close();
    }
    Integer t = sc.nextInt();
    while(t>0)
    {
    int m = sc.nextInt();
    int n = sc.nextInt();
    int prime[]= new int[n+1];
    for(int i=0;i<=n;i++)
    {
        prime[i]=1;
    }
    prime[0]=0;
    prime[1]=0;
    for(int i=2;i<Math.sqrt(n);i++)
    {
        if(prime[i]==1)
        {
            for(int j=2;i*j<n;j++)
            {
                prime[i*j]=0;
            }
        }
    }
    for(int i=m;i<n;i++)
    {
        if(prime[i]==1)
        {
            System.out.println(i+" ");
        }
    }
    System.out.println();
    t--;
   }
    }
   }
$('#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");
	
  }
});
});

答案 3 :(得分:0)

尝试一下,我在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();
            }
        })
    }
相关问题