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

4

12

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.

Talley answered 14/9, 2010 at 20:9 Comment(0)
B
15

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.

Bedfordshire answered 14/9, 2010 at 20:13 Comment(0)
C
10

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.

Chiastic answered 14/9, 2010 at 20:26 Comment(0)
D
2

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

Discoverer answered 14/9, 2010 at 20:14 Comment(11)
Says who? I have to be absolutely sure, if I will use it in production.Benuecongo
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)Discoverer
Says the HTML spec: "The control names/values are listed in the order they appear in the document." w3.org/TR/html401/interact/forms.html#form-content-typeChiastic
@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. @Chiastic thanksBenuecongo
@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).Severen
@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 problemUnbelieving
@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'.Benuecongo
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 :)Discoverer
@Vaidas The funny thing is that serious applications fail because of such simple assumptions :)Benuecongo
@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.Discoverer
@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?Unbelieving
H
2

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.

Hydatid answered 14/9, 2010 at 20:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.