FORM和POST数组中Checkbox的顺序是否相同?

时间:2022-01-13 13:22:53

I have several checkboxes and I am trying to take a lazy approach of inserting them into the DB.

我有几个复选框,我试图采取懒惰的方法将它们插入到数据库中。

So I wanted to know, would the order of the checkbox array (checkboxes[]) in the POST super array be in the order that they are in my html page?

所以我想知道,POST超级数组中复选框数组(checkboxes [])的顺序是否按照它们在我的html页面中的顺序排列?

If not, then I will just stop being a lazy developer!

如果没有,那么我将不再是一个懒惰的开发人员!

Thanks for any help.

谢谢你的帮助。

3 个解决方案

#1


9  

Yes, see 17.13.4 Form content types of the HTML 4 specification. For application/x-www-form-urlencoded:

是,请参阅17.13.4 HTML 4规范的表单内容类型。对于application / x-www-form-urlencoded:

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

控件名称/值按它们在文档中出现的顺序列出。名称与值分隔=,名称/值对通过&分隔。

And for multipart/form-data:

对于multipart / form-data:

The parts are sent to the processing agent in the same order the corresponding controls appear in the document stream.

部件以相同的顺序发送到处理代理,相应的控件出现在文档流中。

The PHP manual states the same (see How do I create arrays in a HTML <form>?):

PHP手册说明了相同的内容(请参阅如何在HTML

中创建数组?):

Specifying an arrays key is optional in HTML. If you do not specify the keys, the array gets filled in the order the elements appear in the form.

在HTML中指定数组键是可选的。如果未指定键,则按照元素在表单中显示的顺序填充数组。

#2


0  

While technically the answer is "Yes", it would still depend on how it's all being accessed, really (and how you're being lazy about it!).

虽然从技术上来说答案是“是”,但它仍然取决于它是如何被访问的,真的(以及你对它的懒惰程度!)。

For example, if the checkbox isn't ticked, then it's not included in the POST. This is because it's not sent by the browser. Yes, the others are still in order, but you have to account for that in your 'laziness'.

例如,如果未勾选复选框,则它不包含在POST中。这是因为它不是由浏览器发送的。是的,其他人仍在秩序中,但你必须在“懒惰”中解释这一点。

Also remember that if you're doing it by using form field names like 'checkbox[1]', 'checkbox[2]' and so on, then these, too, may not be in the order that you expect them to be, depending on how you access it in the PHP.

还要记住,如果你是通过使用'checkbox [1]','checkbox [2]'之类的表单字段名称来完成它,那么这些也可能不符合你期望的顺序,取决于您在PHP中如何访问它。

Doing a "while $i < $number_of_known_checkboxes" then looking at "$checkbox[$i]" would be fine. However using "while $i < count($checkbox)" would fail (as checkbox is only populated with checked checkboxes) and a foreach loop could also present you with issues.

做一个“while $ i <$ number_of_known_checkboxes”,然后看“$ checkbox [$ i]”就行了。但是使用“while $ i ($>

#3


0  

There's one thing to be wary of. Whatever the HTML 4 spec might say, they are submitted in the order they appear in the DOM.

有一点需要警惕。无论HTML 4规范如何说,它们都按照它们在DOM中出现的顺序提交。

Have a look at this (invalid) HTML.

看看这个(无效的)HTML。

<html>
<head>
    <title>Test Page</title>
</head>
<body>
    <form name="f1" method="get" action="fostered.htm">
        <label>A <input type="checkbox" name="a" checked="checked" /></label>
        <table>
            <tbody>
                <tr>
                    <td>
                        <label>B <input type="checkbox" name="b" checked="checked" /></label>
                    </td>
                </tr>
                <tr>
                    <label>C <input type="checkbox" name="c" checked="checked" /></label>
                </tr>
            </tbody>
        </table>
        <input type="submit" />
    </form>
</body>
</html>

In this case, the order on submission is A, C, B because during parsing, the C checkbox is "foster parented" to be before the beginning of the table because it is not inside a table cell.

在这种情况下,提交的顺序是A,C,B,因为在解析期间,C复选框是“foster parented”,它位于表的开头之前,因为它不在表格单元格内。

Of course, this would normally show up on the rendered page, but CSS could mask that.

当然,这通常会显示在渲染页面上,但CSS可以掩盖它。

Another good reason to take the trouble to validate your HTML.

麻烦地验证HTML的另一个好理由。

It's also worth noting that "hidden" input fields are not foster parented, so if C had been a hidden field, the order on submission would be A, B, C.

值得注意的是,“隐藏”输入字段不是寄养父母,因此如果C是隐藏字段,则提交的顺序将是A,B,C。

#1


9  

Yes, see 17.13.4 Form content types of the HTML 4 specification. For application/x-www-form-urlencoded:

是,请参阅17.13.4 HTML 4规范的表单内容类型。对于application / x-www-form-urlencoded:

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

控件名称/值按它们在文档中出现的顺序列出。名称与值分隔=,名称/值对通过&分隔。

And for multipart/form-data:

对于multipart / form-data:

The parts are sent to the processing agent in the same order the corresponding controls appear in the document stream.

部件以相同的顺序发送到处理代理,相应的控件出现在文档流中。

The PHP manual states the same (see How do I create arrays in a HTML <form>?):

PHP手册说明了相同的内容(请参阅如何在HTML

中创建数组?):

Specifying an arrays key is optional in HTML. If you do not specify the keys, the array gets filled in the order the elements appear in the form.

在HTML中指定数组键是可选的。如果未指定键,则按照元素在表单中显示的顺序填充数组。

#2


0  

While technically the answer is "Yes", it would still depend on how it's all being accessed, really (and how you're being lazy about it!).

虽然从技术上来说答案是“是”,但它仍然取决于它是如何被访问的,真的(以及你对它的懒惰程度!)。

For example, if the checkbox isn't ticked, then it's not included in the POST. This is because it's not sent by the browser. Yes, the others are still in order, but you have to account for that in your 'laziness'.

例如,如果未勾选复选框,则它不包含在POST中。这是因为它不是由浏览器发送的。是的,其他人仍在秩序中,但你必须在“懒惰”中解释这一点。

Also remember that if you're doing it by using form field names like 'checkbox[1]', 'checkbox[2]' and so on, then these, too, may not be in the order that you expect them to be, depending on how you access it in the PHP.

还要记住,如果你是通过使用'checkbox [1]','checkbox [2]'之类的表单字段名称来完成它,那么这些也可能不符合你期望的顺序,取决于您在PHP中如何访问它。

Doing a "while $i < $number_of_known_checkboxes" then looking at "$checkbox[$i]" would be fine. However using "while $i < count($checkbox)" would fail (as checkbox is only populated with checked checkboxes) and a foreach loop could also present you with issues.

做一个“while $ i <$ number_of_known_checkboxes”,然后看“$ checkbox [$ i]”就行了。但是使用“while $ i ($>

#3


0  

There's one thing to be wary of. Whatever the HTML 4 spec might say, they are submitted in the order they appear in the DOM.

有一点需要警惕。无论HTML 4规范如何说,它们都按照它们在DOM中出现的顺序提交。

Have a look at this (invalid) HTML.

看看这个(无效的)HTML。

<html>
<head>
    <title>Test Page</title>
</head>
<body>
    <form name="f1" method="get" action="fostered.htm">
        <label>A <input type="checkbox" name="a" checked="checked" /></label>
        <table>
            <tbody>
                <tr>
                    <td>
                        <label>B <input type="checkbox" name="b" checked="checked" /></label>
                    </td>
                </tr>
                <tr>
                    <label>C <input type="checkbox" name="c" checked="checked" /></label>
                </tr>
            </tbody>
        </table>
        <input type="submit" />
    </form>
</body>
</html>

In this case, the order on submission is A, C, B because during parsing, the C checkbox is "foster parented" to be before the beginning of the table because it is not inside a table cell.

在这种情况下,提交的顺序是A,C,B,因为在解析期间,C复选框是“foster parented”,它位于表的开头之前,因为它不在表格单元格内。

Of course, this would normally show up on the rendered page, but CSS could mask that.

当然,这通常会显示在渲染页面上,但CSS可以掩盖它。

Another good reason to take the trouble to validate your HTML.

麻烦地验证HTML的另一个好理由。

It's also worth noting that "hidden" input fields are not foster parented, so if C had been a hidden field, the order on submission would be A, B, C.

值得注意的是,“隐藏”输入字段不是寄养父母,因此如果C是隐藏字段,则提交的顺序将是A,B,C。