简体   繁体   English

邮寄形式的数据顺序是否与网络表单中的相同?

[英]Will data order in post form be the same to it in web form?

Assuming there are 5 inputs in web form 假设网络表格中有5个输入

<input name='the_same[]' value='different' />
<input name='the_same[]' value='different' />
<input name='the_same[]' value='different' />
<input name='the_same[]' value='different' />
<input name='the_same[]' value='different' />

When server side receive the post data, i use a foreach to accept data, say 当服务器端收到帖子数据时,我会使用foreach接受数据

$the_same = new array();
foreach($_POST['the_same'] as $data)
    $the_same[] = $data;

Will the order of data saved in server side be the same to it in web form? 服务器端保存的数据顺序是否与Web表单中的顺序相同? and cross browsers, it could be a criteria all browsers follow. 和跨浏览器,它可能是所有浏览器遵循的标准。

Well, the W3C recommendation on HTML forms does say: 那么, W3C关于HTML表单的建议确实说:

The control names/values are listed in the order they appear in the document. 控件名称/值按它们在文档中出现的顺序列出。

Still, I'd consider it a bit risky to have your app depend critically on that detail. 不过,我认为让你的应用程序严格依赖于这个细节有点冒险。

PHP already handles converting POSTed/GETed variables into arrays when you put [] after the name. 当您在名称后放置[]时,PHP已经处理将POSTed / GETed变量转换为数组。 Do that instead of getting it wrong yourself. 这样做而不是自己弄错了。

Better way to do in html: 在html中更好的方法:

<input name='the_same[]' value='different' />

Then in server: 然后在服务器中:

$the_same = new array();
foreach($_POST['the_same'] as $data) // or $_GET if you prefer
    $the_same[] = $data;

In this way no variable will be overwrite. 这样就不会覆盖任何变量。

if you want to have it in an order, you may use the dynamic variables or simply access the array explicitly 如果你想在订单中使用它,你可以使用动态变量或只是显式访问数组

the_same1 the_same2 the_same3 the_same1 the_same2 the_same3

since you know the names anyway, you can access them easily 既然你知道这些名字,你可以轻松访问它们

$the_same = array();
for($i=1; ; $i++){
    $tmp =$_REQUEST["the_same".$i]
    if( empty($tmp) ){
            // no more stuff
            break;
    }
    $the_same[] = $tmp;
}

If you change the name of your input to the_same[] - $_REQUEST['the_same'] will become an array of those values, first to last in element order (all current browsers I believe). 如果您将输入的名称更改为the_same[] - $_REQUEST['the_same']将成为这些值的数组,首先按元素顺序排列(我相信所有当前浏览器)。

You can also specify a specific order if you need, or even use string keys. 如果需要,您还可以指定特定订单,甚至可以使用字符串键。 For instance, an <input name='the_same[apple][2]'/> would become $_REQUEST['the_same']['apple'][2] 例如, <input name='the_same[apple][2]'/>将成为$_REQUEST['the_same']['apple'][2]

Without using the [] on the input names, PHP will only see the last value. 如果不在输入名称上使用[] ,PHP将只看到最后一个值。 The other values will be 'overwritten' by the later value when the $_REQUEST / $_GET / $_POST arrays are constructed. 构造$_REQUEST / $_GET / $_POST数组时,其他值将被后一个值“覆盖”。

An example of using that to your advantage could be with a checkbox, as the HTML checkbox only submits a value when checked, you may want to submit a "not checked" value somtime: 使用它的一个示例可能是使用复选框,因为HTML复选框仅在选中时提交值,您可能希望提交“未检查”值somtime:

<input type='hidden' name='check' value='not checked' />
<input type='checkbox' name='check' value='checked' />

Most likely yes, but you should not assume this. 很可能是的,但你不应该假设这一点。 It depends on your browser how the inputs are being send, and aditionally PHP does not guarantee that a foreach loop iterates in the same order as the elements were added. 这取决于您的浏览器如何发送输入,并且通常PHP不保证foreach循环以与添加元素相同的顺序迭代。

It is a bad practise to give your inputs the same name. 为您的输入提供相同的名称是一种不好的做法。

You could append an index after each name value (even with javascript if you want), and then read this in PHP to be sure the order is maintained. 您可以在每个名称值后附加一个索引(如果需要,甚至使用javascript),然后在PHP中阅读此内容以确保维护订单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM