vote up 0 vote down star

Hi All,

This is a similar question to this one: http://stackoverflow.com/questions/713884/convert-js-array-to-json-object-for-use-with-jquery-ajax

Except that I have an object that has several arrays in it.

Object looks like (simulated):

{"Users":[1,2,3,4], "Clients":[5,6,7,8], "CompletionStatus":"pastdue", "DateRange":"thisweek"}

and is created like so:

            Filter = new FilterData;

            Filter.Add(9, "Clients");
            Filter.Add(12, "Clients");
            Filter.Add(75, "Clients");

            Filter.Add(9, "Users");
            Filter.Add(12, "Users");
            Filter.Add(75, "Users");

            Filter.SetValue("DateRange", "yesterday");

	function FilterData(){

		this.Users = [];

		this.Clients = [];

		this.Options = [];
		this.Options.CompletionStatus = [];
		this.Options.DateRange = [];

		this.Add = function(id, type){
			this[type].push(id);
			this[type] = this[type].unique();
			return;
		}

		this.Rem = function(id, type){+
			this[type].splice( Filter[type].indexOf(id), 1);
			this[type] = this[type].unique();
			return;
		}

		this.SetValue = function(key, value){
			this.Options[key] = value;
		}

	}

...

If I just do:

AjaxRequest = $.ajax({
...
data: Filter,
...
});

it seems that obj will end up like: ...Users=1&Users=2&Users=3&....

This is causes PHP to only see one value for Users, which will be the last one, in this case 3.

where what I need for PHP to see the arrays properly is: ..Users[]=1&Users[]=2&Users[]=3&....

Any idea how to correct this?

Example:

In firebug, my post looks like this:

Clients 1
Clients 10
CompletionStatus    pastdue
DateRange   14
Users   2
Users   3
Users   4

and my response looks like this:
page: <?php print_r($_POST) ?>

Array
(
    [Users] => 4
    [Clients] => 10
    [CompletionStatus] => pastdue
    [DateRange] => 14
)
flag

76% accept rate
1  
Can you give an example of what 'obj' actually looks like? – Darrell Brogdon Dec 11 '09 at 20:48
Actually, the better question is, how are you building 'obj'? – Darrell Brogdon Dec 11 '09 at 20:52
@Darrell Brogdon: Edited. – Eli Dec 11 '09 at 21:02

2 Answers

vote up 2 vote down

Change the name of Users to Users[] in the javascript. 'Users[]' is a valid property name for a javascript object:

var o= { 'Users[]': 'hello user' }; 
alert(o['Users[]']);
link|flag
vote up 2 vote down

Would it be possible for you to just use the built-in param method?

http://docs.jquery.com/Internals/jQuery.param

That seems to do what you want. It also has a few more cool additions in the jQuery 1.4alpha if you want the bleeding edge version.

link|flag
Great minds think alike, but for some reason, param() does the same thing. One value only. – Eli Dec 11 '09 at 21:11
I get different results, check this out: jsbin.com/uvige – Alex Sexton Dec 11 '09 at 22:14
Should work now with jQuery >= 1.4. "As of jQuery 1.4, the $.param() method serializes deep objects recursively to accommodate modern scripting languages and frameworks such as PHP and Ruby on Rails." - api.jquery.com/jQuery.param – Jordan Brough Jan 29 at 5:33
Yep! It wasn't out quite yet, but now 1.4.2 probably isn't far off - no changes on the param front in that one though. Check out the jQuery BBQ plugin for the "deparam" method that can accompany the native param method. – Alex Sexton Jan 29 at 9:08

Your Answer

Get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.