php 删除前的javascript删除确认

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16153078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:37:22  来源:igfitidea点击:

javascript delete confirmation before deleting

phpjavascriptmysql

提问by user2288273

I am displaying a JavaScript confirmation dialog that asks if the user is sure they wish to delete the record. However, even when the user clicks "yes" the query is still executed and the record is still deleted what can be the problem here is the code:

我正在显示一个 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>

So even if I click cancel the query/record gets deleted. How can I stop this from happening what am I doing wrong? Still a newbie at JS!:(

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

回答by gpinkas

You must use the return value of the confirm dialog:

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

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>

See the changes in "onclick" and "deleletconfig".

请参阅“onclick”和“deleletconfig”中的更改。

回答by Bashir Noori

Create a function and call it:

创建一个函数并调用它:

<script type="text/javascript">
    function confirmation() {
      return confirm('Are you sure you want to do this?');
    }
</script>

<a href="#" onclick="return confirmation()">Delete</a>

回答by chandresh_cool

Instead of

代替

if (del==true){

do

if(del) {

回答by Richard

add return = true for deletion, return = false for no deletion.

add return = true 表示删除,return = false 表示不删除。

回答by Garth Baker

Just copy and paste

只需复制和粘贴

it gives an alert box to confirm whatever you want your link to do.

它提供了一个警告框来确认您希望链接执行的任何操作。

Alert returns true or false based on selected answer, and then will either do what you've set your link to do or will not do what your link is supposed to do.

Alert 根据选定的答案返回 true 或 false,然后将执行您设置链接要做的事情,或者不做您的链接应该做的事情。

<td>
   <a class="delete_button" href="#">Delete</a>
</td>

<script type="text/javascript">
    $('.delete_button').click(function(e){
        var result = confirm("Are you sure you want to delete this user?");
        if(!result) {
            e.preventDefault();
        }
    });
</script>