javascript删除前删除确认

use*_*273 0 javascript php mysql

我正在显示一个JavaScript确认对话框,询问用户是否确定要删除该记录.但是,即使用户单击"是",仍然执行查询并且仍然删除记录,这里的问题可能是代码:

<script>
    function deleletconfig() {
        var del=confirm("Are you sure you want to delete this record?");
        if (del==true){
            alert ("record deleted")
        } else {
            alert("Record Not Deleted")
        }
    }
</script>
Run Code Online (Sandbox Code Playgroud)

因此,即使我单击取消,查询/记录也会被删除.我怎么能阻止这种情况发生我做错了什么?还是新手JS!:(

gpi*_*kas 8

您必须使用确认对话框的返回值:

echo"<form method = \"post\" action =\"change.php?change=$productid\">";
echo// form fields here!...
echo"<input type=\"submit\" name = \"delete\" value=\"Delete\" onclick=\"return deleletconfig()\" />";

if (isset($_POST['delete'])){  //delete clicked
//get variables here
//run query delete record from xyz where id=$id


}

<script>
    function deleletconfig(){

    var del=confirm("Are you sure you want to delete this record?");
    if (del==true){
       alert ("record deleted")
    }else{
        alert("Record Not Deleted")
    }
    return del;
    }
</script>
Run Code Online (Sandbox Code Playgroud)

查看"onclick"和"deleletconfig"中的更改.