Page 1 of 1

Posting Objectified Data Using jQuery

Posted: Mon May 31, 2010 3:04 am
by JellyFish
jQuery question. This:

Code: Select all

$.post('somepage.php', {key:'value'});
makes sense. However, this:

Code: Select all

$.post('somepage.php', {key:['array','value']});
doesn't to me. What does it mean to send data like this? I thought that the object was turned into a query string like:

[text]key=value&...etc[/text]

What does it mean to send a key which equals an array? As far as I know, a query string can't contain a set of key/values for a key:

[text]key=[arrary,value]&...[/text]

Can it? Will someone please shed some light on this?

Re: Posting Objectified Data Using jQuery

Posted: Mon May 31, 2010 4:09 am
by Weirdan
What does it mean to send a key which equals an array?
jquery would then pass multiple instances of key, one for each value:

Code: Select all

key=array&key=value
This is mostly useless, unless you use php's array notation for urlencoded data:

Code: Select all

$.post('somepage.php', {'key[]':['array','value']});
If you do, it will look like an array to the receiving php script.

Re: Posting Objectified Data Using jQuery

Posted: Mon May 31, 2010 6:26 am
by JellyFish
Oh so if you set a key twice in the query string, the server script (php) will receive it as an array?

Useful notation in jQuery. However, why must it be:

Code: Select all

$.post("/", {'key[]':['value1', 'value2']}):
and not just

Code: Select all

$.post("/", {'key':['value1', 'value2']}):
???

Re: Posting Objectified Data Using jQuery

Posted: Mon May 31, 2010 9:41 am
by Weirdan
JellyFish wrote:Useful notation in jQuery. However, why must it be:

Code: Select all

$.post("/", {'key[]':['value1', 'value2']}):
and not just

Code: Select all

$.post("/", {'key':['value1', 'value2']}):
???
Because you need to name your field names with [] postfix for php to recognize them as arrays. And jQuery not doing this for you because it's not limited to php-backed sites.

Re: Posting Objectified Data Using jQuery

Posted: Tue Jun 01, 2010 11:00 pm
by JellyFish
Oh, so then the actual post data will look like:

[text]key[]=value1&key[]=value2[/text]

correct?

Re: Posting Objectified Data Using jQuery

Posted: Wed Jun 02, 2010 4:22 am
by Weirdan
JellyFish wrote:correct?
Yep.