PDA

View Full Version : Ext.getFormData



TommyMaintz
31 Jan 2007, 2:55 PM
As i already though Animal showed me it was useless, you could just use YUI's setForm although that will give you some crap you dont need. Maybe this comes in handy when Jack will get rid of YUI's utilities completely

Hey, I created a method to retrieve a hashstring for a form.

You can pass it a div id containing form elements or a form id or the actual element itself.
It returns a uriencoded hashstring like for example foo=bar&lorem=ipsum....

Also the form elements within the passed element dont need a name attribute. If it can't find a name it uses the elements id.

Basically its a stripped version of YUI's setForm method, but you wont have to use their Connect function. This means you can do something like:


view.load({
url: 'blabla.php',
params: Ext.getFormData('form-id')
});


Here is the code, if you have any suggestions or you think this method is useless, please tell me!



Ext.getFormData = function(form){
form = Ext.get(form);
var elements = form.dom.elements != undefined ? form.dom.elements : form.select('input, submit, select, file, textarea').elements;
var el, name, value, disabled, formData='';
var hasSubmit = false;

for (var i=0; i<elements.length; i++){
el = elements[i];
disabled = el.disabled;
name = (el.name && el.name.length > 0) ? el.name : el.id;

value = el.value;

if(!disabled && name)
{
switch (el.type)
{
case 'select-one':
case 'select-multiple':
for(var j=0; j<el.options.length; j++){
if(el.options[j].selected){
if(Ext.isIE){
formData += encodeURIComponent(name) + '=' + encodeURIComponent(el.options[j].attributes['value'].specified?el.options[j].value:el.options[j].text) + '&';
}
else{
formData += encodeURIComponent(name) + '=' + encodeURIComponent(el.options[j].hasAttribute('value')?el.options[j].value:el.options[j].text) + '&';
}

}
}
break;
case 'radio':
case 'checkbox':
if(el.checked){
formData += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&';
}
break;
case 'file':
case undefined:
case 'reset':
case 'button':
break;
case 'submit':
if(hasSubmit == false){
formData += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&';
hasSubmit = true;
}
break;
default:
formData += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&';
break;
}
}
}

return formData.substr(0, formData.length - 1);
};

JeffHowden
31 Jan 2007, 5:06 PM
Also the form elements within the passed element dont need a name attribute. If it can't find a name it uses the elements id.

Per the HTML spec, form controls without a name can not be considered successful and therefore should not be sent to the server.

Animal
1 Feb 2007, 12:18 AM
You don't have to use Connect.asyncRequest to actually send the data if all you want is a uurlencoded string of parameters:

Just use:



view.load({
url: 'blabla.php',
params: YAHOO.util.Connect.setForm('form-id')
});


You're going to have the Connect object in your page anyway, so you might as well use their code which does exactly the same thing!