Fuczi Fuczi Fuczi Fuczi - 13 days ago 5
HTML Question

Call a javascript function before action in html Form

Can I intercept a form submit action and do some JavaScript function before sending it to post/get?

For example:

<form action="something.php" method="post">
<input type="text" value="Some data"/>
<input ... ... ... />
<input type="submit" value="Send"/>

</form>


Is there a way I can call a JavaScript function before calling action when I click the submit button?

javascriptFunction();
//now go to form action...

Answer
    <form action="something.php" method="post" onsubmit="return myFunction()">
 <input type="text" value="Some data"/>
 <input ... ... ... />
 <input type="submit" value="Send"/>

<script type="text/javascript">
 function myFunction(){
//do something here
//if you want to submit form
  return true;
//if don't want to submit
  return false;
  } 
</script>
Comments