我有一个小问题。我为用户提供删除数据库记录的选项。但首先我想为他们提供一个 jquery 确认框,询问他们是否确定要删除此特定记录。
问题是,当用户在确认框中单击“取消”时,我的 php 代码仍然执行我的 sql 删除查询。
如何阻止此代码在取消选择时执行,并允许其在确定选择时执行?
提前致谢,我的代码如下:
HTML
<div id="remove">
<a href="myaccount.php?deletebook=' . $bookID . '" data-value="' . $customerName . '">delete</a>
</div>
JQUERY
$(document).ready(function(){
$('#remove a').click(function(){
var customerName = $(this).data('value');
return confirm("Do you really want to delete your book with the name: "+customerName+"?");
});
});
PHP
if(isset($_GET['deletebook'])){
$bookID = trim(strip_tags($_GET['deletebook']));
$bookID = mysql_real_escape_string($bookID);
$sql50 = "DELETE FROM books WHERE bookID='$bookID' LIMIT 1";
$result50 = mysql_query($sql50) or die(mysql_error());
$sql51 = "DELETE FROM pages WHERE bookID='$bookID'";
$result51 = mysql_query($sql51) or die(mysql_error());
header("location: myaccount.php");
}
最佳答案
将你的jquery更改为:
$(document).ready(function () {
$('#remove a').click(function (e) {
var customerName = $(this).data('value');
var r = confirm("Do you really want to delete your book with the name: " + customerName + "?");
if (r != true) {
e.preventDefault();
}
});
});
关于php - Jquery确认框在取消选择后仍然执行php代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22049253/