Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Several Checkboxes sharing the same name

According to the w3c "Several checkboxes in a form may share the same control name. Thus, for example, checkboxes allow users to select several values for the same property." However, if you do that, PHP will only take the last value. For example:

<?php
if ($_POST) {
echo "<pre>";
print_R($_POST);
echo "</pre>";
}
?>
<form action="" method = "post">
<input type="checkbox" name="pet" value="dog" />Dog<br />
<input type="checkbox" name="pet" value="Cat" />Cat<br />
<input type="checkbox" name="pet" value="bird" />bird<br />
<input type="checkbox" name="pet" value="iguana" />iguana<br />
<input type="submit" />
</form>

If you submit that form, you will see that only the checked box that appears last will be set. The browser sends them all, but they overwrite each other. So, setting the same name to several checkboxes can cause problems. Has it always been like that? I seem to remember that it was possible to actually send all the values as an array.

I know that you can just add an [] at the end of the name to create an array of values:

<?php
if ($_POST) {
echo "<pre>";
print_R($_POST);
echo "</pre>";
}
?>
<form action="" method = "post">
<input type="checkbox" name="pet[]" value="dog" />Dog<br />
<input type="checkbox" name="pet[]" value="Cat" />Cat<br />
<input type="checkbox" name="pet[]" value="bird" />bird<br />
<input type="checkbox" name="pet[]" value="iguana" />iguana<br />
<input type="submit" />
</form>

But the w3c doesn't specify that. Honestly I don't remember if I always used the [] at the end of the name, but for some reason I think at some point I didn't. Was there any time in the past when you could make it work without the []?

http://www.w3.org/TR/html401/interact/forms.html#checkbox

like image 793
Buzu Avatar asked May 14 '13 20:05

Buzu


People also ask

Can multiple checkboxes have same name?

If you submit that form, you will see that only the checked box that appears last will be set. The browser sends them all, but they overwrite each other. So, setting the same name to several checkboxes can cause problems.

Can checkbox have same ID?

It is not valid HTML to have multiple elements with the same ID.

Can you group checkboxes?

You can add checkboxes to a document or template, and group them together to choose how many boxes must be checked. To group checkboxes, click and drag the selection box around the checkboxes you'd like to group and then click Group checkboxes in the right sidebar. F.

What is multi checkbox?

Zend\Form\Element\MultiCheckbox is meant to be paired with the FormMultiCheckbox for HTML inputs with type "checkbox". This element adds an InArray validator to its input filter specification in order to validate on the server if the checkbox contains values from the multiple checkboxes.


2 Answers

That would never have worked without the [], not in PHP.

W3C don't specify anything about how query strings are handled server-side. (Ignoring an irrelevant, obsolete corner of the CGI spec, only relevant to PHP in that it was a security hole up until recently).

It looks like that pattern is valid markup, but not commonly used, for the reason you describe.

A similar pattern is used for radio buttons, of which only one can be selected at a time. (In fact, giving the radio inputs the same name is how the browser knows to treat them as a group). Perhaps that's what you were thinking of.

like image 65
sourcejedi Avatar answered Oct 20 '22 14:10

sourcejedi


If you really want it in PHP, try this:

<?php

if (count($_POST)) {
  header("Content-type: text/plain");
  $fp = fopen("php://input", "r");
  fpassthru($fp);
  fclose($fp);
  exit;
}

?>
<form action="" method = "post">
<input type="checkbox" name="pet" value="dog" />Dog<br />
<input type="checkbox" name="pet" value="Cat" />Cat<br />
<input type="checkbox" name="pet" value="bird" />bird<br />
<input type="checkbox" name="pet" value="iguana" />iguana<br />
<input type="submit" />

</form>

More on php://input stream can be found in PHP documentation.

like image 2
Vedran Šego Avatar answered Oct 20 '22 13:10

Vedran Šego