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

前端 未结 4 1301
南笙
南笙 2020-12-20 13:41

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

相关标签:
4条回答
  • 2020-12-20 13:52

    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.

    0 讨论(0)
  • 2020-12-20 14:04

    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.

    0 讨论(0)
  • 2020-12-20 14:08

    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.

    0 讨论(0)
  • 2020-12-20 14:11

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

    0 讨论(0)
提交回复
热议问题