What is the expected array order represented in the HTML form?

I am wondering if there is any guarantee on the order of the POST variables that I will see on the server side.

My use case: I have a form that the user will fill out to enter a list of names and letters. I use 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 string can be cloned via javascript to allow the user to enter more names and letters, so I will not know in advance how many will be submitted.

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

+8
javascript html php forms
Sep 14 '10 at 20:09
source share
4 answers

why not add a grouped key, for example:

 <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 cloning based on the current number. Thus, everything is already minimized.

+12
Sep 14 '10 at 20:13
source share

What is the expected array order presented in the HTML form?

According to the HTML specification:

Control names / values ​​are listed in the order that they appear in the document

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

However, it is better to use encoding to use an indexed array approach, as shown in prodigitalson's answer.

+7
Sep 14 '10 at 20:26
source share

The data will appear in the same order as in the form. So, the first line has the key 0, the second line - 1.

+1
Sep 14 '10 at 20:14
source share

As Vaidas Zilionis said, the data will be displayed in the same order as in the form, see W3C HTML 4.01 Specification :

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

multi-part / form data
[...] The message "multipart / form-data" contains a number of parts, each of which represents a successful control. Parts are sent to the processing agent in the same order as the corresponding controls are displayed in the document stream .

0
Sep 14 '10 at 20:25
source share



All Articles