Sweetalert在删除之前在php中确认

时间:2019-04-10 01:44:11

标签: php sweetalert

我想使用sweetalert的确认警报。但是我遇到一些问题,当我单击“是”时,即使我已经调用了delete函数,我的代码也无法正常工作。我唯一的问题是关于我的查询的SWAL一切正常。现在这是我的代码

这是我的user_del.php

    <?php session_start();
if(empty($_SESSION['id'])):
header('Location:../index');
endif;
include("../dist/includes/dbcon.php");
$id=$_REQUEST['id'];
$result=mysqli_query($con,"UPDATE accounts_at SET status = 0 WHERE id ='$id'")
    or die(mysqli_error());
        if ($result !== false) {
 echo "<script type='text/javascript'>alert('Successfully archived a account!');</script>";
    echo "<script>document.location='index'</script>";
    }

?>

function archiveFunction(id) {
    event.preventDefault(); // prevent form submit
    swal({
        title: "Are you sure?",
        text: "You will not be able to recover this imaginary file!",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: '#DD6B55',
        confirmButtonText: 'Yes, delete it!',
        cancelButtonText: "No, cancel plx!",
        closeOnConfirm: false,
        closeOnCancel: false
    }).then(function (isConfirm) {
        if (isConfirm) {
            swal("Deleted!", "Your imaginary file has been deleted!", "success");
            window.location = 'user_del.php?id='+id;
        }
        else {
            swal("Cancelled", "Your imaginary file is safe :)", "error");
        }
    });
};
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
 <a id="removeme" onclick="archiveFunction(1)" href="#">Click to archived the data
</a>

1 个答案:

答案 0 :(得分:0)

这是因为您使用的是$ _REQUEST,

由于您使用的是window.location = 'user_del.php?id='+id; GET方法,因此需要使用$ _GET来获取ID

请在 user_del.php

上尝试此操作
<?php session_start();
if(empty($_SESSION['id'])):
header('Location:../index');
endif;
include("../dist/includes/dbcon.php");
$id=$_GET['id'];
$result=mysqli_query($con,"UPDATE accounts_at SET status = 0 WHERE id ='$id'")
    or die(mysqli_error());
        if ($result !== false) {
 echo "<script type='text/javascript'>alert('Successfully archived a account!');</script>";
    echo "<script>document.location='index'</script>";
    }

?>