POSTing arrays of checkboxes

I’m giving myself a crash course in PHP. I’m puzzled by one particular bit of advice in my PHP textbook and I was hoping for a second opinion.

Imagine a table of personal details for the user to enter on a single page. What I ultimately want is a form like this, albeit dynamically generated.

When the user clicks ‘Submit’, all the personal details entered in the table will be submitted, along with the state of all the checkboxes. There is a variable number of rows in the table of personal details (in practice only 10-20).

The question is how to submit the array of checkboxes and text boxes. My PHP book suggests doing things this way for the checkboxes:

<input type="checkbox" name="select_name[]" value="1" checked /> <input type="checkbox" name="select_name[]" value="2" checked /> ...However surely this

<input type="checkbox" name="select_name[1]" checked /> <input type="checkbox" name="select_name[2]" checked /> ...is simpler?

  1. The process of accessing the checkbox values in PHP seems to be simpler in the second case:

$my_array = $_POST['select_name']; echo $my_array[1];Whereas in the first case, I have to worry about the array possibly being empty before I access individual elements.
2. The first method doesn’t work for text boxes. So the second method has the advantage of consistent syntax between accessing the text boxes and accessing the checkboxes.

Am I missing something?
Is the advantage solely that, with method 1, you can ascertain the number of checkboxes checked? (if we didn’t already know that server-side?)

To confuse things further, after Googling the subject, I’ve seen a few suggestions that to submit arrays of text boxes, you have to do something contrived with hidden forms, or serialise the array then ‘de-serialise’ it again (?!) … so I have a niggling concern that there might be a problem with the way I’m doing it…?

Thanks in advance!

This is what I do:

<input type="checkbox" name="select_name[0]" value="1" checked="checked">
<input type="checkbox" name="select_name[1]" value="1" checked="checked">

PHP:

[php]
$select_name = array();
if (isset($_POST[‘select_name’])) { $select_name = $_POST[‘select_name’]; }

foreach ($select_name AS $key => $value) {
echo “select_name[”.$key."] is ".$value;
}
[/php]

The foreach() loop runs for every checked checkbox (so it skips the unchecked ones, as they don’t exist in the $_POST superglobal). For checkboxes, it is pretty much mandatory to set the value attribute (what you make of it however, doesn’t really matter, as long as there IS a value).

Besides that, don’t think too lightly about accessing user input. Malicious form-spoofing users may send you unexpected values, and your script needs to be able to handle them correctly (ignore them, isolate them, send an error, as long as they don’t use them as though they were valid input).

Thanks for your reply. I’m still a little puzzled as to the purpose of the value=“1” on both lines of your example. Are you saying you can put any old thing in there?

I’m trying to get to the bottom of why the example in my PHP book says select_name[] (i.e. without an index). Is there an advantage of putting the array index in the value field instead?

I think it’s allowed to leave the index out, but just to be on the safe side, I always put it in. And the only correlation between the name attribute and the value attribute is that the name is used as the $_POST index on which the value will be stored. Putting the index in the value attribute will not do you good, but not putting a value in won’t either. As long as it’s a string, or integer, you can put whatever you want in there.

Thanks for your explanation.

This still leaves me puzzled as to why, in my PHP book, they do it ‘the first way’ (see my original post).

I just want to be clear about whether there is some advantage of the ‘empty brackets’ approach that I’m overlooking! :) It’s so counter-intuitive – and inconsistent with the way you post an array of, say, text boxes – that it leaves me thinking there must be a good reason he did it that way…

Well, it does save you the trouble of having to keep track of which number comes next … and it saves a byte per index (after index 9, two bytes) on your bandwidth … but that’s really all I could think of.

I’d like to go the ‘complete’ way and just add it, but I wouldn’t be tempted to use arrays in form elements too fast anyway, giving them a descriptive name is easier on anyone who’ll handle your script when, say, you’re leaving the company/project/get a car accident/etc.

Sponsor our Newsletter | Privacy Policy | Terms of Service