Posts Tagged ‘ajax’

Butterfish
by Butterfish

Ajax: Form Serialization

Posted in Javascript, Swell

When using Ajax to submit a form, you’ll have to attach as args the input fields name/value combination; this task can be very boring, especially if your form takes three pages…
This is where the form serialization feature strikes, by simply specifying the form(s) name to your XHR handle, the serializer will parse through your form and assemble each content input field into an object that’ll be passed automatically to your request.

We made a choice that come very handy in our case, every serialized form is returned in a global object and once requested, “stringified”.
Why using JSON?
In some case, let’s say… in “multiples select”, we can come up with multiple values, and when using GET you’ll definitely do something like key=val, with multiple values, key[]=val, but how much server-side languages can handle this kind of GET property? not much… whereas much languages are now able to unserialize JSON.

PHP is able to “decipher” passed arrays in GET, but what about the others languages? If there’s Java/Ruby/ASP.NET folks out there, we would like to hear from you!

XHR in Element

In order to save both time and lines, it was quite interesting to think of a way to implement XHR in our Element class, take a look at these working drafts:

// well, let's say we have two forms
var forms = $('foo','bar');
 
// will send them separately asychronously
forms.xhr();
 
// this time we want to harmonize their form[method] value
forms.xhr('POST');
 
// or their form[action] value
forms.xhr(null, 'foo.php');
 
// two concurrent ajax request is somehow too much, we'd better merge them!
forms.xhr('POST', 'foo.php', true);