How to create an Ajax, validated form
Thanks to jQuery and its plugins, it is possible to change the way those forms are handled, in order to obtain slick effects, and an overall better user experience.
In this tutorial, we'll associate the validation plugin, and the form plugin, to get a contact form that is validated before submission, and submitted via ajax. Why don't you give it a try?
Demonstration
How to setup
Step 1: File inclusion
As this method works with jQuery, several jQuery files need to be included before we can move on, the jQuery library of course, the validation plugin, that will check that the required form elements are correctly filled in before submission, and the form plugin, that will handle the Ajax submission.
/** jQuery Inclusion **/ <script type="text/javascript" src="Path_to/jQuery.js"></script> /** Plugin Inclusion **/ <script type="text/javascript" src="Path_to/jquery.validate.js"></script> <script type="text/javascript" src="Path_to/jquery.form.js"></script>
Step 2: The Form Markup
Once the files are ready to be used, we create the html markup of a very simple form containing 4 elements: a pseudo, an email address, a website and a comment area:
<form id="comment_form" action="/postcomment" method="post"> <label for="pseudo">Pseudo</label> <input id="pseudo" name="pseudo" class="required" type="text" /> <label for="mail">Email:</label> <input id="mail" name="mail" class="required email" type="text" /> <label for="website">Website:</label> <input id="website" name="website" type="text" /> <label for="message">Message:</label> <textarea id="message" name="message" class="required"></textarea> <input class="button" type="submit" value="Send" /> </form>
As for usual forms, on submission, the data will be posted to the page specified in your action attribute, in our case, the page '/postcomment'.
Please note that the class required has been added to each input that must be filled by the user before submission.
If you wish to get more information about the validation plugin, you'll find some on its dedicated page.
Step 3: The Javascript Validation and Submission
In this particular case, the javascript is the place of main attention. The principle is quite simple really.
As you can see in the following code, we setup the javascript validation, which means that every input having the class 'required' will be checked (line 2). Then, line 3, we change what happens when the form is submitted. Meaning, instead of having a normal submit, we'll have an Ajax submit (line 4):
jQuery(document).ready(function(){ jQuery("#comment_form").validate({ //Validation submitHandler: function(form) { //Event fired on form submission jQuery("#comment_form").ajaxSubmit(function(data){ //Ajax Submit jQuery("#comments_list").prepend(data); //Result display //You can clear the form at this stage if you want }); } }); });
After the form submission, what we need to do is to display the result to the user because as it is sent via Ajax, the page won't be refreshed and the data processing will be done behind the scene.
Step 4: The Ajax result
The result returned by the page '/postcomment' is held by the variable data (line 4 of the jQuery code). Knowing that, you can use your '/postcomment' page to retrieve the post variables as you would do for a normal page, for example:
}
Because what you output here (as an echo for example) is going to be available in your javascript, you can choose between several formats to build the final result that will be showed to the user.
The best practise option is to write a json array with the posted values:
{ "PSEUDO":"'.$comment_pseudo.'", "EMAIL":"'.$comment_mail.'", "MESSAGE":"'.$comment_message.'", "WEBSITE":"'.$comment_website.'" }
This json array will then be available in your javascript, where you can create the comment markup from it, and insert this markup in the page when ready. The jQuery code becomes then:
jQuery(document).ready(function(){ jQuery("#comment_form").validate({ //Validation submitHandler: function(form) { //Event fired on form submission jQuery("#comment_form").ajaxSubmit(function(data){ //Ajax Submit data = JSON.parse(data); // Get the json returned var comment='<div class="comment">'; comment+=' <div class="comment_msg">'+data.MESSAGE+'</div>'; comment+=' <div class="comment_footer">'; comment+=' From: <a href="'+data.WEBSITE+'">'+data.PSEUDO+'</a>'; comment+=' </div>'; comment+='</div>'; jQuery("#comments_list").prepend(comment); //Result display //You can clear the form at this stage if you want }); } }); });
As we've said, the variable data holds the result returned by the page '/postcomment', which in our case is the json array (line 5). From this array, we then write the html markup (line 6-11), and we insert it in the page (line 12).
If you are not that at ease with json, it is still possible to write your html inside the postcomment page and to output it. It will therefore be all ready to go when reaching your javascript (remove line 5 then), but it's a slower option.
Summary
Creating an Ajax submitted form is actually very similar to how you would do normally as the jquery form plugin does most of the hard work for you. When a user will hit the submit button, the validation plugin will check that the required input are present and will leave the form plugin to handle the Ajax submission. You can then retrieve the post data as you would normally do and work with them to output the result to the user.
















