Page 1 of 1

[SOLVED] Javascript onSubmit question

Posted: Tue Apr 12, 2005 5:57 am
by Jean-Yves
Hi, I want to get a page to prompt the admin user before they delete some games from my online game.

For some reason, I cannot get the onSubmit to trigger correctly:

In the <head> of the page, I have:

Code: Select all

<script type="text/javascript">
    function confirmDelete(){  
      return (confirm("Are you quite sure that you wish to delete the selected game(s)?"));
    }
  </script>
This should be triggered by the submit button for the following form:

Code: Select all

<form name="frmStalledGames" id="frmStalledGames" action="delete.php" method="post" onSubmit="confirmDelete();">
However, in Firefox, the delete.php script is always fired as soon as I hit the submit button. I have tried replacing the "return " line with an alert to see if the function is even called, but it is not.

In IE, the comfirm prompt is displayed but irrespective of which button is clicked (OK or Cancel), the delete script is run.

I'm sure that it's something really obvious, I just can't see it right now.

Thanks in advance for any help that you can offer.

Posted: Tue Apr 12, 2005 7:32 am
by Chris Corbyn
The idea is to return either TRUE (submits the form) or FALSE (stops the submission), depending upon whether OK was clicked or not.

Clicking OK, returns TRUE, clicking cancel returns FALSE.

Code: Select all

<script type=&quote;text/javascript&quote;>
<!-- Hide
function confirmDelete() {
    if (confirm('Are you sure you want to do this?')) {
        return true;
    } else {
        return false;
    }
}
// -->
</script>
<form onSubmit=&quote;confirmDelete()&quote;>

Posted: Tue Apr 12, 2005 7:44 am
by Jean-Yves
I'd already tried that longhand syntax, but effectively it gives the same result - ie it returns true or false in both cases.

What I don't get is why the submit is not cancelled in IE when Cancel is clicked, and why in FF the function is ignored completely...?

Strange.

Posted: Tue Apr 12, 2005 7:47 am
by Chris Corbyn
Apolgies.... not quite with it today ;-)


You need to put "return" in the onSubmit so it picks up the value passed.

Code: Select all

<script type=&quote;text/javascript&quote;>
<!-- Hide
function confirmDelete() {
    if (confirm('Are you sure you want to do this?')) {
        return true;
    } else {
        return false;
    }
}
// -->
</script>
<form onSubmit=&quote;return confirmDelete()&quote;>

Posted: Tue Apr 12, 2005 7:58 am
by Jean-Yves
Thanks mate, I owe you one! :D

Posted: Tue Apr 12, 2005 7:58 am
by Jean-Yves
And the FF issue was caching. A good cache clearout and it was a happy bunny again.