Jump to content

php deleting record with js confirmation


man5

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.