Bug Tracker

Ticket #2377 (closed bug: invalid)

Opened 6 years ago

Last modified 6 years ago

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:
Blocking: Blocked by:

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.

Change History

comment:1 Changed 6 years ago by flesler

  • Component changed from core to ajax

comment:2 Changed 6 years ago by flesler

  • Owner set to flesler
  • Status changed from new to assigned

comment:3 Changed 6 years 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.

comment:4 Changed 6 years ago by flesler

Related to #2207.

Note: See TracTickets for help on using tickets.