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 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题