doudun8705 2014-01-13 13:46
浏览 26
已采纳

表单中的HTTP排序变量

given a form like the following

<form action="/page" method="POST>
    <input type="hidden" name="input" value="12" />
    <input type="hidden" name="input" value="24" />
</form>

Using Google Chrome 31.x and PHP 5.5, /page now has a $_POST variable for input of 24

This happens because when the $_POST array is created, The value is over written in the array. And the latter value is the value which is preserved.

Most browsers Ive tested this is the case, But Is there any HTTP spec / browser spec which says that form inputs should be sent in the order they are defined ? Or could an update in the future (or an old browser) send these updates in the reverse order for example ? or a random order ?

Edit:

to give more context, It will not be used like the above in all cases. only in a certain case.

The first form element is a SELECT box, But depending on the options chosen, Javascript will be able to change the value, Without changing the Select box value

  • 写回答

2条回答 默认 最新

  • donglinyi4313 2014-01-13 13:48
    关注

    Regardless of the order in which HTTP sends your two values, PHP can only have one value for $_POST['input'].

    To solve this, use array notation:

    <form action="/page" method="POST>
        <input type="hidden" name="input[]" value="12" />
        <input type="hidden" name="input[]" value="24" />
    </form>
    

    Now you'll have an array $_POST['input'] with both values.

    To answer your question about the spec, see this page:

    http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4

    See the bullets for the application/x-www-form-urlencoded default content type.

    The control names/values are listed in the order they appear in the document. The name is separated from the value by '=' and name/value pairs are separated from each other by '&'.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口