I'm making a form that requires me to use a varying amount of checkboxes. 开发者_Go百科Anywhere from 0 to n. I can easily insert the value I want into the value="" of the checkbox, so that's not a problem.
My question is - how would I write a PHP form that can take any amount of checkbox values from 1 to n, and then loop through all of those values?
I would do this for your HTML
<input type="checkbox" name="checkbox[]" value="your_supplied_value1" />
<input type="checkbox" name="checkbox[]" value="your_supplied_value2" />
if (isset($_POST['checkbox'])) {
foreach ($_POST['checkbox'] as $c) {
// do something
}
}
use the array type for the name
name="name[]"
This passes an array to $_POST['name'] which you can foreach through
Something like this:
<?php
for($i = 0; $i < $n; $i++) {
?><input type="checkbox" name="checkthisbox[<?=$i?>]" value="value" /> checkbox :)<?php
}
?>
then loop through them like this:
<?php
foreach ($_POST['checkthisbox'] as $checkbox) {
var_dump($checkbox);
}
?>
untested, but something like this should work
精彩评论