Jquery确认框在取消选择时仍然执行php代码

时间:2014-02-26 17:49:06

标签: php jquery html

我有一点点问题。我正在为用户提供删除db记录的选项。但首先我想向他们提供一个jquery确认框,询问他们是否确定要删除此特定记录。

问题是,当用户在确认框中单击取消时,我的php代码仍会执行我的sql删除查询。

如何阻止此代码在取消选择上执行,并允许它在OK选择上执行?

提前致谢,我的代码如下:

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");
     }

4 个答案:

答案 0 :(得分:3)

将你的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();
        }

    });
});

答案 1 :(得分:0)

您的代码可以正常使用fiddle

$(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+"?");
    });
});

答案 2 :(得分:0)

$(document).ready(function () {
    $('#remove a').click(function () {
        var customerName = $(this).data('value');
        if(confirm("Do you really want to delete your book with the name: " +customerName+ "?")){
          // YOUR DELETE CODE
        }
    });
});

答案 3 :(得分:0)

如果你使用普通的JavaScript确认框,你的代码应该可以正常工作。如果您使用any将原生js确认框转换为自定义框

,则共享相关插件