Reputation: 83
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");
}
Upvotes: 1
Views: 331
Reputation: 1889
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();
}
});
});
Upvotes: 3
Reputation: 61
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
Upvotes: 0
Reputation: 4869
$(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
}
});
});
Upvotes: 0
Reputation: 133
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+"?");
});
});
Upvotes: 0