Bug Tracker

Opened 15 years ago

Closed 15 years ago

Last modified 15 years ago

#2377 closed bug (invalid)

jQuery.param returns wrong data when using brackets "[ ]" in variables

Reported by: Sincklation Owned by: flesler
Priority: major Milestone: 1.3
Component: ajax Version: 1.2.3
Keywords: param Cc:
Blocked by: Blocking:


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
			// 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 ) );
					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)

comment:1 Changed 15 years ago by flesler

Component: coreajax

comment:2 Changed 15 years ago by flesler

Owner: set to flesler
Status: newassigned

comment:3 Changed 15 years ago by flesler

Resolution: invalid
Status: assignedclosed

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:

  print_r( $_POST );
  print_r( $_GET );

Tried this from firefox 2 but I assume it works the same on any browser.

comment:4 Changed 15 years ago by flesler

Related to #2207.

Note: See TracTickets for help on using tickets.