Recently I was asked how submit a form with JavaScript… and I didn’t have a solid answer
for them. The shame! So, like a teacher having the class write ‘I will not talk in class’
over and over as punishment, here are a few various ways to submit a basic form in
JavaScript.
The Standard Form Submit
Here is the basic form we’re working with:
Standard Form Submit
1234567891011
<html><body><formname="submit_with_button"action="success.html"><fieldset><legend>Submit form with button</legend><labelfor="email">Email Address</label><inputtype="text"id="email"name="email"/><inputtype="submit"value="Submit"/><p>Standard method with a submit button</p></fieldset></form></body></html>
Submit with a JavaScript Function
In this example we call a JavaScript function from the link, and that function
submits the form. This method is fine, but it’s generally bad form to put
JavaScript functions in a link’s href.
Submit with a JavaScript Function
123456789101112131415
<html><body><formname="submit_with_js"action="success.html"><fieldset><legend>Submit form with JavaScript</legend><labelfor="email">Email Address</label><inputtype="text"id="email"name="email_js"/><aid="submit_js"href="javascript: submitform()">Submit</a></fieldset><script>functionsubmitform(){document.submit_with_js.submit();};</script></form></body></html>
Submit with jQuery
This method still depends on the action tag, but just triggers the submit()
function. Use return false to stop the link being executed.
Submit with jQuery
1234567891011121314151617
<html><body><formname="submit_with_jquery"action="success.html"><fieldset><legend>Submit form with jQuery</legend><labelfor="email">Email Address</label><inputtype="text"id="email"name="email_jquery"/><aid="submit_jquery"href="#">Submit</a></fieldset><script src="http://code.jquery.com/jquery-latest.js"></script><script>$('#submit_jquery').click(function(){$('form[name="submit_with_jquery"]').submit();returnfalse;});</script></form></body></html>
Submit with jQuery AJAX
Using the jQuery post function here (a subset of $.ajax()), we submit the form
asynchronously and return back any resulting data. In this example I’m
returning back the success message in an alert. I’m also using
javascript:void(0) in the submit link href rather than a #. This
way, even if the link is executed, the URL isn’t modified and the page doesn’t
jump.
AJAX Form Submit
12345678910111213141516171819202122232425
<html><body><formname="submit_with_jquery_ajax"action="success.html"><fieldset><legend>Submit form with jQuery AJAX</legend><labelfor="email">Email Address</label><inputtype="text"id="email"name="email_ajax"/><aid="submit_ajax"href="javascript:void(0)">Submit</a></fieldset><script src="http://code.jquery.com/jquery-latest.js"></script><script>$('#submit_ajax').click(function(){//Grab action from the form if it's thereif($('form[name="submit_with_jquery_ajax"]').attr('action')){myAction=$('form[name="submit_with_jquery_ajax"]').attr('action');}else{myAction='success.html';};$.post(myAction,function(data){alert(data);});returnfalse;});</script></form></body></html>