Onsubmit not working!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • immohito
    New Member
    • Jun 2012
    • 6

    Onsubmit not working!

    My onsubmit function is not working.

    My PHP file has two forms. Both form tags have an onsubmit attribute that links to the same javascript file. The first form's onsubmit is working. The function being called by the second form has been simplified to this:

    Code:
    function UserDataCheck()
    {
    return false;
    }

    The form tag is

    Code:
    echo "<form name='user_form' method='post' onsubmit='return UserDataCheck();'>"
    Please help. This is driving me crazy! Earlier I was doing some data checking in the UserDataCheck() but I stripped all of that away to see if I could even use onsubmit to stop the user from submitting! NO SUCCESS.
  • Claus Mygind
    Contributor
    • Mar 2008
    • 571

    #2
    Why don't you reverse your onsubmit button. You never reach the function call UserDataCheck() ; because the return statement comes first. Or just remove the "return" all together.

    onsubmit = "UserDataCheck( ); return false;"

    A better option is not to include two statements in the onsubmit. You already have stated in the function you don't want a return so just call the function UserDataCheck() ; , it is really bad form to put 2 statements into the "onsubmit" because of the way that browsers work by racing ahead and not waiting for a function to finish and return.

    Personally, I don't use the <input type="submit />. I set the form action to "return false;". Then I define a simple button with a call to the function I want to run.

    Code:
    <form method=post onSubmit="return false;">
    <input type="button" value="Submit" onClick="UserDataCheck();"/>
    </form>
    Or if you want to be a little more current instead of assigning the onClick an action you would assign an event listener. Using the code below the form loads first then the javaScript runs and assigns an event listener in this case the "click" to the button. For my own convenience, I store all my variables in a global object "g" so when I am debugging it is easy to look for my unique stuff. But this is not really needed.

    Code:
    <head>
    <script type="text/javascript">
    g = new Object();
    
    function docStartUp()
    {
      if (document.getElementById('Save'))
      {
        g.Save = document.getElementById('Save');
        g.Save.addEventListener("click", function(){g.process.saveData(this.form)}, false);
    	}
    }
    </script>
    </head>
    <body onLoad = "docStartUp();">
      <form method="post" action="serverSideCore.php">
         <input type="button" id="Save" value="Save"/>
      </form>
    </body>

    Comment

    Working...