My Love Of Jquery’s Form Plugin
The type of web programing I do often has a large database back-end with which requires user input and data return. In the olden days you just created an html form and then posted some input data to some cgi (say PHP script) and the script would speak with the database and return some html causing the browser to reload on post. Now, that we are a super cool web 2.0 world we do things differently. We make our friend javascript use Ajax to do the post for us. If you are anything like me you take one look at javascript and say “no thank you”. Most people hate the syntax of javascript, the confusion of the language, or the browser compatibility issues; when I say most people I am including me. This is where our friends at Jquery come to the rescue http://jquery.com (more on them in another post).
For now I want to highlight a jquery plugin, an additional library that grows the functionality of jquery. The Form Plugin (Hosted Here) allows jquery to submit form data via Ajax either when the user hits submit using the ajaxForm() or on demand using ajaxSubmit(). Here is an example of how I used it in a recent project.
index.php -
So from the above html code you can see there is no submit button. Our goal here is to make it so when you change anyone of the input form items it will collect the value of each field and post the data to returnGraph.php. The data will be posted via Ajax so there is no page refresh. We want Ajax to change the graph and put the graph in the div with id “graph-return”. We also want some sort of spinner to spin to show the user the browser is thinking and waiting for a graph to appear. We are going to put an animated gif in the graph div that will be replaced with the graph when it is returned from returnGraph.php.
The next step is the write some javascript. You need to make sure you have the jquery and form plug in included in your code.
Now in the background the ajaxSubmit() function sends the serialized form Post to returnGraph.php. Also, just before posting we clear the “graph-return” div and put an automated gif in place to let the user know something is happening. Once the script is done processing the data it will call the “success” part ajaxSubmit() and pass in the response from the post. In our case it is a graph. The animated gif is replaced with the graph. Ajax Submit complete.
From the code below you can see we bind a change action to each input item in the form. So anytime anything changes in the fields we redraw the graph.
$(”#control-form :input”).change(resubmitGraph);









