请如何显示确认删除对话框按钮。因此,如果用户单击“是”,它将继续删除,否则将不会删除该条目。

这是我的代码,可以正常工作,但是我不知道如何使其弹出“您确定要删除对话框”

The HTML button

 <a class='btn btn-danger delete_data' id="<?php echo $matched1; ?>">Blast</a>

 <script type="text/javascript">
        $(document).ready(function(){
            $(".delete_data").click(function(){
                var del_id = $(this).attr('id');
                $.ajax({
                    type: 'POST',
                    url: 'delete.php',
                    data: 'delete_id='+del_id,
                    success: function(data){
                        if(!data){
                            window.location.replace("memberpage.php");
                        }
                        else{
                            alert('failed');
                        }
                    }
                });
            });
        });
    </script>


delete.php

$username = $_POST['delete_id'];
    $stmt = $db->prepare("DELETE FROM users WHERE username='".$username."'");
    $stmt->execute();


谢谢你帮我

最佳答案

您可以使用confirm()如下:

$(".delete_data").click(function(){
    var choice = confirm('Do you really want to delete this record?');
        if(choice === true) {
            var del_id = $(this).attr('id');
            $.ajax({
                type: 'POST',
                url: 'delete.php',
                data: 'delete_id='+del_id,
                success: function(data){
                    if(!data){
                        window.location.replace("memberpage.php");
                    }
                    else{
                        alert('failed');
                    }
                }
            });
        }else{
            return false;
        }
});


参考:-https://www.w3schools.com/js/js_popup.asp

关于php - 在删除数据库中的行之前显示确认对话框,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42540381/

10-14 14:42
查看更多