What is the difference between these two form submission methods?
<form method="post" action="gert.php" onsubmit="return validate()">
<p>Enter an email address:</p>
<input id='email'>
<button type='submit' id='validate'>Validate!</button>
</form>
<form method="post" action="gert.php" onSubmit='validate(); return false;'>
<p>Enter an email address:</p>
<input id='email'>
<button type='submit' id='validate'>Validate!</button>
</form>
I’m using the first one in submitting forms and returning false in the JavaScript validate() function if the form is not filled out. Just now in another website, I have seen the second one being used. I am unable to figure out the difference, so I want to know if I’m using still old ways in doing the things or if both mean the same thing? I was thinking in the second way, form will be submitted only if thisform.submit statement is executed. I came to know this when I executed the form but I still want to know what is the major difference between the two?
The first example will only ever be submitted if the response from
validate()is truthy.The second example will validate the form (call
validate()) but it will never be submitted, as a falsy value is sent back to the event handler function will cancel the default browser behaviour.