Helping ordinary people create extraordinary websites!
   

How to submit a form using JavaScript?

Wednesday, 18th November 2009
by RileyJ

I would like to use JavaScript to submit my webpage form. How can this be done?

Thanks.





Vader
Hi Riley,
To submit a form using JavaScript you can use a few different techniques. Here are three of them:

JavaScript submit code separate from your form
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
<script type="text/javascript">
function submit_it()
{
document.MyForm.submit();
}
</script>

<form name="MyForm" method="post" action="FileToSubmitTo.php">
<input type="text" name="firstname" value="">
<a href="javascript: submit_it()">Submit</a>
</form>

JavaScript submit code included in form code
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
<form name="MyForm" method="post" action="FileToSubmitTo.php">
<input type="text" name="firstname" value="">
<a href="javascript:document.MyForm.submit();">Submit</a>
</form>

JavaScript submit with button using onclick
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
<form name="MyForm" method="post" action="FileToSubmitTo.php">
<input type="text" name="firstname" value="">
<input type="button" name="Submit" value="Submit" onclick="document.forms.MyForm.submit()">
</form>

Vader
Monday, 30th November 2009
Votes:
16
10

More JavaScript Help:
» How can I create a select all link for my text area with JavaScript?
» How to validate a variable has letters in it
» How to send post data to the server without loading a new page?
» Phone number validation
» How can I do a for loop in JavaScript?
» How to add a floating sidebar like on the Mashable website?
» How can I create Facebook applications?
» How to remove special characters from a string?