表单输入值查询字符串顺序

It is OK to have form inputs with the same name:

<form>
    <input type="text" name="email">
    <input type="text" name="email">
</form>

A GET action on the form produces the following query string: ...?email=&email=

Because emails exist for their own, thats fine.

Now, if there is data that belongs together (like to the same person):

<form>
    <input type="text" name="first_name">
    <input type="text" name="last_name">

    <input type="text" name="first_name">
    <input type="text" name="last_name">
</form>

The following query string is generated: ...?first_name=&last_name=&first_name=&last_name=

I guess that the parameter ordering in the query string is the same as the elements appear within the html... nevertheless, I am not sure if this is always the case.

Because the query string is essentially a key/value map, ordering should not matter...

I could add an index to each name but that would require to known the last index everytime I would add a new pair on the client.

Ideally I would like to avoid having to add an index by myself to keep the structure more dynamic.

Are there some better strategies to avoid relying on the order of the query string parameters? Array notation on the name seems to be one way that is used for PHP but I would like to have a more generic way (not using PHP on the server)...

I guess that the parameter ordering in the query string is the same as the elements appear within the html... nevertheless, I am not sure if this is always the case.

It is.

Because the query string is essentially a key/value map, ordering should not matter...

It isn't … although you can treat it like one.

Are there some better strategies to avoid relying on the order of the query string parameters?

Depending on the order is perfectly fine.

Array notation on the name seems to be one way that is used for PHP but I would like to have a more generic way (not using PHP on the server)...

That's another acceptable solution. There are libraries which implement PHP-style query parsing available for other languages.


You could also encode the data using JSON … but then you need to add client-side JavaScript as a dependancy.