Bug Tracker

Ticket #2377 (closed bug: invalid)

Opened 10 months ago

Last modified 4 months ago

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

Reported by: Sincklation Assigned to: flesler
Type: bug Priority: major
Milestone: 1.3 Component: ajax
Version: 1.2.3 Keywords: param
Cc: Needs: Review

Description

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.

Attachments

Change History

Changed 7 months ago by flesler

  • owner deleted
  • component changed from core to ajax

Changed 7 months ago by flesler

  • status changed from new to assigned
  • owner set to flesler

Changed 4 months ago by flesler

  • status changed from assigned to closed
  • resolution set to invalid
  • milestone changed from 1.2.4 to 1.3

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:

<?php
  print_r( $_POST );
  print_r( $_GET );
?>

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

Changed 4 months ago by flesler

Related to #2207.

Note: See TracTickets for help on using tickets.