Difference form submission in Javascript – follow up
In December 2008 I wrote an article introducing the concept of Difference forum submission in Javascript. We received some positive responses from this article and some requests for examples of exactly how this would work. This article is intended to provide a more practical view of the concept.
What is difference form submission?
The quick explanation of the concept is upon form submission unchanged fields are disabled (using Javascript) to prevent them submitting back to the server. The advantages of doing this can include reduced request sizes and the prevention of “clobbering” data in applications where multiple users are editing the same records.
For the full explanation see the original Difference forum submission in Javascript post.
Example 1
This first example is fairly simple. It includes a single Javascript function that is called when the submit event of the form is fired. The form has an example of every field type (that I could think of) so you can see which fields get submitted and which are disabled. The forms have a method attribute of “get” so for this example you will be able to see the submitted form field values in the URL.
When the submit button is clicked the diff() Javascript function is called and the form object is passed to it. The function then loops over all the elements in the form checking if they have changed by comparing the appropriate properties (value, defaultValue, checked, defaultChecked, selected, defaultSelected). If the elements have not changed they will be disabled.
Note that there are certain field types not listed in the function on purpose including hidden, button and reset. The field types not listed will obviously never be disabled.
Example 2 – jQuery plugin
This second example works the same as the first. The difference is that the Javascript has been rewritten so it is now a working jQuery plugin. The plugin currently has an “addFormDiff” method and a “formDiff” function. The “addFormDiff” method is used to bind the formDiff function to the submit event of the form.
I will also note that this is the first time I have written a jQuery plugin (or even used jQuery believe it or not) so any feedback relating to the plugin itself is welcomed.
The jQuery plugin will also be stored at http://plugins.jquery.com/project/formdiff
Testing of the code
All of the code above was tested in the latest versions of Internet Explorer 6, Internet Explorer 7, Firefox 3, Konqueror, Safari, Opera and Chrome so it should be compatible with most of the browsers you are likely to need.
What’s next
There are possible scenarios where you would need to group certain fields together (if one changes they both submit). The code does not currently support this as a feature so it is one possible future addition. There may also be other features that could be added to the library or plugin.
That is all for now. If you find this useful or use any of the above code in your projects please let us know by leaving a comment of sending me an email. As usual all thoughts and feedback is appreciated.
