Forum Moderators: open

Message Too Old, No Replies

Conditional Execution

         

calmseas

6:28 pm on Jan 14, 2011 (gmt 0)

10+ Year Member



In the following code snippet, the user clicks to delete an entry. A confirmation dialog pops up. If he/she does not confirm the delete, the user just wants to dismiss the dialog box. However, in the snippet, the item is deleted regardless of the dialog choice. What is the industry standard for just dismissing this dialog box?

<head>
<script language="Javascript" type="text/javascript">
function disp_confirm() {
if ( !confirm("Are you sure you want to delete this item?")) {
Just dismiss the confirm dialog box ;
}
}
</script>
</head>
<body>
<form action="delete.php" method="post">
<input type="submit" onclick="disp_confirm()" value="Delete">

coopster

6:42 pm on Jan 14, 2011 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



return false will stop the event

Fotiman

6:44 pm on Jan 14, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You are not stopping the event from performing the default action. I would change it slightly to something more like this.

Note language="Javascript" is invalid. Don't include it.


<head>
<script type="text/javascript">
function disp_confirm() {
return confirm("Are you sure you want to delete this item?");
}
</script>
</head>
<body>
<form action="delete.php" method="post" onsubmit="return disp_confirm();">
<input type="submit" value="Delete">


Note the addition of "return" in both the inline event handler and in the function. Returning false will stop the submit from performing the default action. Also note that I've moved it from an onclick event handler on the submit button to the onsubmit action of the form.

Hope that helps.

calmseas

10:16 pm on Jan 14, 2011 (gmt 0)

10+ Year Member



Excellent..........thanks to you both. Always nice to learn something new.