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.
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.
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.
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.
Yes, see 17.13.4 Form content types of the HTML 4 specification. For application/x-www-form-urlencoded:
And for multipart/form-data:
The PHP manual states the same (see How do I create arrays in a HTML
<form>
?):