Javascript 在一个 PHP 文件中确认删除(在 href 上)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2665413/
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-23 01:22:38  来源:igfitidea点击:

Javascript Confirm Delete in One PHP File (on href)

phpjavascriptmysqlconfirmation

提问by Craig Hooghiem

<p><span class="linky"><a href="deletephone.php?id=' . $row['id'] . '">Delete Phone</a></span></p><br />

I have the above code that I am using to link to a delete script. I want to somehow incorporate Javascript with a simple onclick confirmation. This way if they choose OK, I can run the code to delete the item from the database, but if they choose Cancel then I can cancel the operation and do nothing.

我有上面的代码,用于链接到删除脚本。我想以某种方式将 Javascript 与简单的 onclick 确认结合起来。这样,如果他们选择“确定”,我可以运行代码从数据库中删除该项目,但如果他们选择“取消”,那么我可以取消操作并且什么都不做。

I have tried a whole variety of functions with changing the window.location to the delete file, and trying to cancel the href= if they choose Cancel, but it always goes to the link regardless of what the user clicks.

我尝试了各种各样的功能,将 window.location 更改为删除文件,并尝试取消 href= 如果他们选择取消,但无论用户单击什么,它总是转到链接。

I would like to be able to keep the delete functions inside the same PHP file if possible, but this is not necessary at all.

如果可能,我希望能够将删除函数保留在同一个 PHP 文件中,但这根本没有必要。

Thanks in advance!

提前致谢!

ASIDE: If there is a simple PHP way to check IF the alert was confirmed or denied, that could work also. Any way to check what the user chooses and then run my simple delete PHP command.

旁白:如果有一种简单的 PHP 方法来检查警报是否被确认或拒绝,那也可以。以任何方式检查用户选择的内容,然后运行我的简单删除 PHP 命令。

回答by T.J. Crowder

If you just want a simple confirmation:

如果您只想简单确认:

<a href="deletephone.php?id=' . $row['id'] . '" onclick="return confirm(\'Really delete?\');">Delete Phone</a>

confirmprompts the user and returns true if they say OK, false if they say Cancel.

confirm提示用户并在他们说 OK 时返回 true,如果他们说 Cancel 则返回 false。

回答by Dumb Guy

You can set the onclickattribute of the link. If the function returns false, the link will not proceed.

您可以设置onclick链接的属性。如果函数返回false,链接将不会继续。

<a href="deletephone.php?id=' . $row['id'] . '" onclick="return confirm('Are you sure?');">Delete Phone</a>

Here, the confirm()function will return trueif the user pressed OK, and falseif the user pressed Cancel.

在这里,如果用户按下 OK 和如果用户按下 Cancel ,该confirm()函数将返回。truefalse

回答by BASIM ASLAM

Just use this code

只需使用此代码

<a href="deletephone.php?id='. $row['id'].'" onclick="return confirm('Are you sure to delete this ?');">Delete Data</a>