views:

162

answers:

3

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?

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

Thanks for any help.

+7  A: 

Yes, see 17.13.4 Form content types of the HTML 4 specification. For 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:

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>?):

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.

Gumbo
Awesome, thanks Gumbo. :)
Abs
A: 

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

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.

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.

Narcissus
A: 

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.

Have a look at this (invalid) 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.

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

Another good reason to take the trouble to validate your 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.

Alohci