[SOLVED] Javascript onSubmit question

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
Jean-Yves
Forum Contributor
Posts: 148
Joined: Wed Jul 02, 2003 2:13 pm
Location: West Country, UK

[SOLVED] Javascript onSubmit question

Post 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.
Last edited by Jean-Yves on Tue Apr 12, 2005 8:00 am, edited 2 times in total.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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;>
User avatar
Jean-Yves
Forum Contributor
Posts: 148
Joined: Wed Jul 02, 2003 2:13 pm
Location: West Country, UK

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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;>
User avatar
Jean-Yves
Forum Contributor
Posts: 148
Joined: Wed Jul 02, 2003 2:13 pm
Location: West Country, UK

Post by Jean-Yves »

Thanks mate, I owe you one! :D
User avatar
Jean-Yves
Forum Contributor
Posts: 148
Joined: Wed Jul 02, 2003 2:13 pm
Location: West Country, UK

Post by Jean-Yves »

And the FF issue was caching. A good cache clearout and it was a happy bunny again.
Post Reply