jQuery.param returns wrong data when using brackets "[ ]" in variables
When i try to serialize a form that have variable names with brackets " var[] ", that i use with PHP because this language recognize that variables as an array, serialize use encodeURIComponent over the variable names so i obtain " var%5B%5D " the brackets encoded, if i send them that way PHP wont recognize the variable "var" as an array, so i remove it manually from jQuery code and now it's working fine.
param: function( a ) {
var s = [];
// If an array was passed in, assume that it is an array
// of form elements
if ( a.constructor == Array || a.jquery )
// Serialize the form elements
jQuery.each( a, function(){
s.push( this.name + "=" + encodeURIComponent( this.value ) );
});
// Otherwise, assume that it's an object of key/value pairs
else
// Serialize the key/values
for ( var j in a )
// If the value is an array then the key names need to be repeated
if ( a[j] && a[j].constructor == Array )
jQuery.each( a[j], function(){
s.push( j + "=" + encodeURIComponent( this ) );
});
else
s.push( j + "=" + encodeURIComponent( a[j] ) );
// Return the resulting serialization
return s.join("&").replace(/%20/g, "+");
}
Maybe there's a better solution, or I'm doing something wrong.
Change History (4)
Owner: |
set to flesler
|
Status: |
new →
assigned
|
Milestone: |
1.2.4 →
1.3
|
Resolution: |
→ invalid
|
Status: |
assigned →
closed
|
Ok, I just tried this with a small php file and it works well. PHP generates an array out of the inputs. I simply did:
Tried this from firefox 2 but I assume it works the same on any browser.