doufu6130 2019-09-02 14:42
浏览 132
已采纳

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

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)...

  • 写回答

1条回答 默认 最新

  • doris20141022 2019-09-02 15:25
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 poi合并多个word成一个新word,原word中横版没了.
  • ¥15 【火车头采集器】搜狐娱乐这种列表页网址,怎么采集?
  • ¥15 求MCSCANX 帮助
  • ¥15 机器学习训练相关模型
  • ¥15 Todesk 远程写代码 anaconda jupyter python3
  • ¥15 我的R语言提示去除连锁不平衡时clump_data报错,图片以下所示,卡了好几天了,苦恼不知道如何解决,有人帮我看看怎么解决吗?
  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?