man5 Posted April 8, 2014 Share Posted April 8, 2014 I am trying to have a js alert box to confirm before deleting a record. Here is the js code. <script> function confirmation() { var answer = confirm("Are you sure?") if (answer){ alert("This record been deleted!") } else{ alert("Not deleted!") } } </script> php code. <a href="delete.php" onclick="confirmation()">Delete record</a> The problem is, even if i click no in the js alert box and the message shows("Not deleted!"), it still deletes the record. Why is it doing that? My second question is, is it possible to css the js alert box? I need to customize it to my own look. Quote Link to comment https://forums.phpfreaks.com/topic/287620-php-deleting-record-with-js-confirmation/ Share on other sites More sharing options...
kicken Posted April 8, 2014 Share Posted April 8, 2014 The problem is, even if i click no in the js alert box and the message shows("Not deleted!"), it still deletes the record. Why is it doing that?You need to cancel the event if the user clicks no. The old method of doing that is to return false from the event handler. The more modern way is to call preventDefault() on the event object. function confirmation(e) { var answer = confirm("Are you sure?") if (!answer){ e.preventDefault(); return false; } } <a href="delete.php" onclick="confirmation(event)">Delete record</a> My second question is, is it possible to css the js alert box? I need to customize it to my own look. No, not with the built in confirm/alert functions. You'd need to use a custom modal dialog such as Jquery UI's .dialog(). Because this type of dialog would be processed asynchronously you need to handle the event differently. Rather than wait for the answer to determine whether to cancel the event, you need to just cancel the event immediately. Then once you get the answer you can either re-trigger the event or perform the delete using Ajax. Quote Link to comment https://forums.phpfreaks.com/topic/287620-php-deleting-record-with-js-confirmation/#findComment-1475433 Share on other sites More sharing options...
man5 Posted April 8, 2014 Author Share Posted April 8, 2014 You need to cancel the event if the user clicks no. The old method of doing that is to return false from the event handler. The more modern way is to call preventDefault() on the event object. function confirmation(e) { var answer = confirm("Are you sure?") if (!answer){ e.preventDefault(); return false; } } <a href="delete.php" onclick="confirmation(event)">Delete record</a> No, not with the built in confirm/alert functions. You'd need to use a custom modal dialog such as Jquery UI's .dialog(). Because this type of dialog would be processed asynchronously you need to handle the event differently. Rather than wait for the answer to determine whether to cancel the event, you need to just cancel the event immediately. Then once you get the answer you can either re-trigger the event or perform the delete using Ajax. Your new js code works great. That does the trick. As for the custom modal dialog, that's interesting. I will take a look and see how to implement them. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/287620-php-deleting-record-with-js-confirmation/#findComment-1475434 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.