I am a Web Developer based in Michigan and I specialize in User Interface Design and Development.  I create functional, aesthetic and user-friendly web applications that adhere to the latest web standards. 

I am currently available for new projects, so hire me!

 

How To Submit a Form with jQuery and AJAX

June 1st 2011

HTML forms make up a large part of the web. They are the primary method for retrieving input from users.  Typically, you fill out the form, click the submit button, and be redirected to a thank you page.  For web applications, this may not be ideal - you may not want the user to leave the page. In this article, I'll show you how to use jQuery's AJAX function to submit a form asynchronously to the server and avoid a redirect.

A Simple Contact Form

Here's an example of a simple contact form (fyi: submitting it won't do anything yet). We'll be submitting this form via AJAX with jQuery.

Your Name:
Your Email:
Your Message:


 

Here's the code for the form:

<form id="ContactForm">
    Your Name: <input type="text" name="name" value="" /><br /> 
    Your Email: <input type="text" name="email" value="" /><br /> 
    Your Message:<br /> <textarea style="width: 200px; height: 100px;" name="message"></textarea> 
    <br /><br /> 
    <input type="submit" name="submit" value="Submit" /><br />
    <div class="form_result"> </div>
</form>

Notice that I gave my form tag an ID of "ContactForm". We'll need this later so that jQuery can find and retrieve all the data contained in the form. Additionally, I've included a div element to which we will dynamically update with the response from the server.  The div has a class of 'form_result' that we can use to tell jQuery where the response from the server should be output to.  The reason I'm using a class identifier here instead of giving the div an id attribute is because on very large forms, you may want the result of the form to be shown at the very top of the form as well as the bottom.  With jQuery, we can select and update multiple elements with the same class.  However, we can only update one element with the same id.  

jQuery's AJAX Function

The syntax for jQuery's ajax function looks like this:

jQuery.ajax(url, [settings]);

A complete reference to this function can be found by clicking on this link:jQuery's AJAX function. There are numerous settings available to use with this function, but we're only going to be using a handful. We'll discuss each setting briefly here: 

  • url: set this to the URL the form will be submitted to (you'll probably want to put in the URL to your form handler). We'll be setting ours to jQuery-ajax-demo.php for demonstration purposes.
  • type: this is the method type for the form; set it to POST or GET. In our case, we'll be using POST.
  • data: this is the data you'll be submitting; it's a standard query string that you typically see after a domain name in the browser's address bar. You'll see that for the most part, jQuery will create the query string for us.
  • success: you can set this to a single callback function, or as of jQuery 1.5 - an array of functions. We'll be setting ours to a single callback function that accepts the response from the server (first parameter of the callback function).

Let's see what a call to jQuery's AJAX function would look like for our demo:

$.ajax({type:'POST', url: 'jQuery-ajax-demo.php', data:$('#ContactForm').serialize(), success: function(response) {
    $('#ContactForm').find('.form_result').html(response);
}});

As you can see, we are setting the 'type', 'url', 'data', and 'success' settings of jQuery's AJAX function. In the 'data' setting, we're using jQuery's serialize() function by supplying the id of the form as the context.

jQuery's serialize() function looks at every field in the supplied context and creates a string of key-value pairs. The 'name' attribute of each of the fields in the form are used as the keys while the actual data contained in the fields become the values. So for example, in the form we created above, our query string (the result of the serialize function) might look like this:

name=Jason&email=me%40example.com&message=hey+what's+up%3F

That's if I submitted the form with Name set to "Jason", Email set to "me@example.com", and Message set to "hey what's up?". The serialize() method also encodes the data so it can be easily transmitted to the server. 

We now have everything we need to submit the form to the server, but we do not have a form handler yet that will intercept the data and do something with it.  

A Simple Form Handler in PHP

Doing anything too fancy is outside the scope of this article, so we're just going to create a simple form handler that intercepts our data and outputs the data as the response.  Take a look at this PHP code:

if(isset($_POST['name'])) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $msg = $_POST['message'];
	
    ?>
    Your Name Is: <?php echo $name; ?><br />
    Your Email Is: <?php echo $email; ?><br />
    Your Message Is: <?php echo $msg; ?><br />
    <?php
    die();
}

Here we are first checking to see if the form was submitted. The result of isset($_POST['name']) will be true if the key 'name' is defined in PHP's $_POST array, and false otherwise. It will be since 'name' is one of the fields on the form that is serialized and submitted via the AJAX request. Next, we retrieve and then output the data from each of the fields: name, email and message. I told you it was simple! In reality though, you'd probably want to validate the data. Although validating data from a form submission is outside the scope of this article, it does give me an idea for an article I could write next!

Putting It All Together

We have everything we need now, we just have to put it all together! The first thing we will do is create a JavaScript function we can call that runs our jQuery AJAX code, here's what that looks like:

function submitForm() {
    $.ajax({type:'POST', url: 'jQuery-ajax-demo.php', data:$('#ContactForm').serialize(), success: function(response) {
        $('#ContactForm').find('.form_result').html(response);
    }});

    return false;
}

You can see we return false from the function. This is very important! When we return false from this function, it will stop the browser from submitting the form and reloading the page or redirecting us. The last we have to do is update the code for our form to call our submitForm() function:

<form id="ContactForm" onsubmit="return submitForm();">
    Your Name: <input type="text" name="name" value="" /><br /> 
    Your Email: <input type="text" name="email" value="" /><br /> 
    Your Message:<br /> <textarea style="width: 200px; height: 100px;" name="message"></textarea> 
    <br /><br /> 
    <input type="submit" name="submit" value="Submit" /><br />
    <div class="form_result"> </div>
</form>

Check out the demonstration by clicking on Demonstration on How To Submit a Form with jQuery and AJAX.  


Discussion


wret
06/01/2011
wret
werrewt
Mr. Raymon
06/01/2011
Mr. Raymon
I apologize for the stupid message "werrewt", user "wret". I was testing the form contact in working.

Thanks for the tutorial ;)
MJ
06/02/2011
MJ
A form element should have "action" and "method" attributes to be standards compliant.

I usually leave these blank and use

$('#ContactForm').click(function (e) {
e.preventDefault();
$.ajax( /*AJAX Code*/ );
});
MJ
06/02/2011
MJ
Correction:



$('#Submit').click(function (e) {
e.preventDefault();
$.ajax( /*AJAX Code*/ );
});
Jason T. Stiles
06/02/2011
Jason T. Stiles
@MJ - Yep, you could do it that way too. You could also attach a submit event to the form handler as well:

$('#ContactForm').submit(function() { /* AJAX Code */ return false; });

When doing it this way, just make sure you return false from the function, otherwise it won't work.
Israel
06/02/2011
Israel
And if I send data and files in the same form
Jeff
11/18/2011
Jeff
Great explanation and examples... thank you!!!!
joshua
02/01/2012
joshua
Thanks for this tutorial. I believe I found a slight bug - you are missing a closing brace in the ajax code sample following the line "Let's see what a call to jQuery's AJAX function would look like for our demo:" - The top line has two opening braces and you only close one of them before the parentheses at the end.
Jason T. Stiles
02/01/2012
Jason T. Stiles
@joshua - thank you! I have fixed it. :)
rajr
02/16/2012
rajr
Very nice, thanks for putting this together for those of us on the learning curve.

Have something to say?