Jump to content

Recommended Posts

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.

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.