views:

86

answers:

4

I'm wondering if there is any sort of guarantee on the order of POST variables I will see on the server side.

My use case is I have a form that a user will fill out to enter a list of names and emails. I'm using a table rows, each of which has two inputs:

<table>
<tr>
<td><input type='text' name='name[]' /></td>
<td><input type='text' name='email[]' /></td>
</tr>
<tr>
<td><input type='text' name='name[]' /></td>
<td><input type='text' name='email[]' /></td>
</tr>
</table>

The row might be cloned via javascript to allow the user to type in more names and emails so I won't know ahead of time how many will be submitted.

On the server side, I see $_POST['email'] and $_POST['name'] set but I am wondering if I can safely assume $_POST['email'][0] will correspond to $_POST['name'][0], $_POST['email'][1] will correspond to $_POST['name'][1], and so on. Some basic testing seem to indicate yes but I'm wondering if there is a guarantee or if I'm just getting lucky.

+8  A: 

why not add a grouping key like:

<td><input type='text' name='user[0][name]' /></td>
<td><input type='text' name='user[0][email]' /></td>
</tr>
<tr>
<td><input type='text' name='user[1][name]' /></td>
<td><input type='text' name='user[1][email]' /></td>

and then manuall set the user indexes when you clone based on the current number. This way everything is already coallated.

prodigitalson
+1  A: 

Data will appear in same order like in form. So first row have key 0, second row - 1.

Vaidas Zilionis
Says who? I have to be absolutely sure, if I will use it in production.
Nikita Rybak
why don't you just test simple form and var_dump result. You will see the result.the result will be always the same as in form html (of course you can change visability position of elements which have no efect)
Vaidas Zilionis
Says the HTML spec: "The control names/values are listed in the order they appear in the document." http://www.w3.org/TR/html401/interact/forms.html#form-content-type
webbiedave
@Vaidas Not even 1000 manual tests will prove correctness of this assumption. Just because conter-examples are rare, doesn't mean they don't exist. @webbiedave thanks
Nikita Rybak
@Vaidas: And you think a single test clarifies everything? Maybe it's just the browser who does it like that, or maybe only a minor version of that browser.. what if it was undefined and left in the hands of the developers? ..You can't use something in production and rely on it if it's not a standard (sometimes not even then).
halfdan
@Nikita if you need to know such information, there is something wrong with your design. Anyway prodigitalson's answer down here can solve such a problem
Col. Shrapnel
@Col Gimme a brake :) I'm not saying I plan to start using this feature right now. But if I did, I would need some assurance it doesn't depend on the browser. Not just 'my buddy said so'.
Nikita Rybak
ok sorry guys i just need'ed to find specification text from w3.com. It just was so simple scenario and i can't imagine what diffrent browser can read form bottom to top or randomly :)
Vaidas Zilionis
@Vaidas The funny thing is that serious applications fail because of such simple assumptions :)
Nikita Rybak
@Nikita,@col prodigitalson answer is good, but it have lot's of issues. Imagine you doing Invoice form (lol my last project with it). You can add as many rows as you need with JS as well delete them, if you need you can add rows order functionality. If you have authors template it just more easy/faster to develop.
Vaidas Zilionis
@Nikita true :)
Vaidas Zilionis
@Vaidas that's another matter. If you add a random number of rows this way, you don't have to be concerned about particular order, do you?
Col. Shrapnel
A: 

As Vaidas Zilionis said, data will appear in exact the same order as they appear in the form, see the W3C's HTML 4.01 Specification:

application/x-www-form-urlencoded
[...] 2. The control names/values are listed in the order they appear in the document.

multipart/form-data
[...] A "multipart/form-data" message contains a series of parts, each representing a successful control. The parts are sent to the processing agent in the same order the corresponding controls appear in the document stream.

Archimedix
+3  A: 

What is the expected order of an array submitted in an HTML form?

According to the HTML specification:

The control names/values are listed in the order they appear in the document

http://www.w3.org/TR/html401/interact/forms.html#form-content-type

However, it's better coding practice to employ an indexed array approach as shown in prodigitalson's answer.

webbiedave