简体   繁体   English

用甜蜜警报 2 删除确认

[英]Delete confirmation with sweet alert 2

I want to delete a DB record with php.我想用 php 删除一个数据库记录。 Before I sumbit a form I want a notification of Sweet Alert (2) with the buttons "Yes of No".在我提交表格之前,我想要一个带有“是或否”按钮的 Sweet Alert (2) 通知。

When "yes" I want to submit the form and activate my php code.当“是”时,我想提交表单并激活我的 php 代码。

Problem is it won't work.问题是它不起作用。 After I click "yes" my page is refreshing and my php code don't start.单击“是”后,我的页面正在刷新并且我的 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>";

}

Modify the form button using an ID like this :使用这样的 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>

And then the script like this :然后是这样的脚本:

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

 $('#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 } ?>

Try this one , I used this in laravel , It works试试这个,我在 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>

and in scripts:并在脚本中:

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

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

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