简体   繁体   中英

Jquery confirm box still executing php code upon cancel selection

I have a little itty bitty problem. I am offering users the option to delete db records. But first I would like to serve them up a jquery confirm box asking them if they are sure they want to delete this particular record.

The problem is that when the user clicks cancel in the confirm box my php code still executes my sql delete queries.

How could I stop this code from executing on a cancel selection, and allow it to execute on an OK selection?

Thanks in advance, and my code is below:

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

Change your jquery to this:

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

    });
});

Your code works fine see 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+"?");
    });
});
$(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
        }
    });
});

If you use, plain JavaScript confirm box, your code should work fine. share the relevant plugin if you use any to transform native js confirm box to custom ones

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM