Several Checkboxes sharing the same name
Asked Answered
T

2

20

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

Tanjatanjore answered 14/5, 2013 at 20:55 Comment(3)
You must use the [] and then you can use in_array to determine which checkboxes have been selected. Annoying, but aren't all forms?Lovellalovelock
The w3c doesn't specify this because it is php specific (and possibly other languages) and not related to the w3c. Other languages do not have this requirement. As you stated, the browser does send all values.Kite
I see both your point. Thanks. And yes, all forms are annoying.Tanjatanjore
C
12

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.

Cozy answered 14/5, 2013 at 21:11 Comment(2)
Perhaps. I know that radio buttons are used like that, and I'm guessing you are right. I was getting both confused.Tanjatanjore
This works in Classic ASP as well. Also it returns only the checked values of the checkboxes.Python
M
3

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.

Morry answered 14/5, 2013 at 21:26 Comment(1)
I'm ok with using the [], I was just curious about it. But I will read that link you provided just because it is always good to be informed.Tanjatanjore

© 2022 - 2024 — McMap. All rights reserved.