I have a servlet with a form and two buttons. One is a Submit button, the other is a Delete button. The default action is Updater.do, but when I click the Delete button, I have a function to change the action to Deleter.do. This works fine. The problem I have now is that when I put in a confirm dialog, if the user clicks “OK”, then it does go on to Deleter.do, as I wanted. However, when they click “Cancel”, instead of just staying on the page, it appears to go to “Updater.do”. How would I remedy this? I tried deleting the default action and having both Submit and Delete call changeAction, but that didn’t work. Here is the javascript chgAction.
function chgAction(action_name) {
if (action_name=="Delete") {
var answer = confirm("Are you sure you want to delete this Person? This action cannot be undone.")
if (answer) { document.forms[0].action = \"Deleter.do\"; }
}
}
If both submit and delete are submit buttons, just give them different names. The name of whichever button is clicked will be sent with the request so the server can sort it out.
When calling the confirm dialog, you must return false from the listener or the form will still submit:
and chgAction must return
falseas suggested by Nivas.