New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include array indexes in $.param() #749

Closed
wants to merge 2 commits into
base: master
from

Conversation

Projects
None yet
2 participants
@levic

levic commented Apr 29, 2013

#208 rewrote support for key[] support inside $.param(), but it doesn't work if you have objects inside arrays:

Consider the case of:

var[][a]=1&var[][b]=2&var[][c]=3

It is ambiguous whether the var[][b] belongs to the [a] object, the [c] object or is an object on its own. PHP at least treats it as a totally new object which means it is impossible to have an array with objects with multiple values. jQuery gets around this by always including the array index, so the above example would become (assuming the [b] belonged to the same object as the [a]):

var[0][a]=1&var[0][b]=2&var[1][c]=3

Demonstration fiddles:
http://jsfiddle.net/DQD3c/1/ (jquery)
http://jsfiddle.net/DQD3c/2/ (zepto)

Attached patch adds the array index back in & updates test cases. traditional argument passing doesn't change, as mentioned in #208 it can't handle nested items anyway.

Levi Cameron
Include array index when serializing nested arrays
Needed for jquery & PHP compatibility
@mislav

This comment has been minimized.

Show comment
Hide comment
@mislav

mislav Apr 29, 2013

Collaborator

It doesn't seem like jQuery always includes the array index. Only when the value is an object. Could you look into that detail?

Collaborator

mislav commented Apr 29, 2013

It doesn't seem like jQuery always includes the array index. Only when the value is an object. Could you look into that detail?

Levi Cameron
Modify .serialize() to more closely match jquery behaviour
For arrays containing scalars, query does not include the final array index
@levic

This comment has been minimized.

Show comment
Hide comment
@levic

levic May 1, 2013

Have updated and added a new test case based on http://jsfiddle.net/DQD3c/4/ (was able to pass the original test cases when I still had code that wasn't consistent with jquery)

In the situation of x : [ [ [ 5 ] ] ](ie if the tree contains only scalars or arrays with 1 element) you could theoretically generate x[][][]=5 but jquery doesn't go in for the extra complexity of doing that so I didn't even try.

levic commented May 1, 2013

Have updated and added a new test case based on http://jsfiddle.net/DQD3c/4/ (was able to pass the original test cases when I still had code that wasn't consistent with jquery)

In the situation of x : [ [ [ 5 ] ] ](ie if the tree contains only scalars or arrays with 1 element) you could theoretically generate x[][][]=5 but jquery doesn't go in for the extra complexity of doing that so I didn't even try.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment