<table>
<col width="250">
<col width="250">
<col width="250">
<tr><td>
<input type="checkbox" name="toppings[]"/>Pepperoni<br>
<input type="checkbox" name="toppings[]"/>Mushroom<br>
<input type="checkbox" name="toppings[]"/>Bacon
</td>
<td>
<input type="checkbox" name="toppings[]"/>Onion<br>
<input type="checkbox" name="toppings[]"/>Tomatoes<br>
<input type="checkbox" name="toppings[]"/>Olives
</td>
<td>
<input type="checkbox" name="toppings[]"/>Pineapple<br>
<input type="checkbox" name="toppings[]"/>Green Pepper<br>
<input type="checkbox" name="toppings[]"/>Extra Cheese
</td>
</tr>
</table>
$toppings = isset($_POST["toppings[]"]) ? $_POST["toppings[]"] : array("");
foreach($toppings as $chk => $selection){
echo $selection . ", ";
}
/*
For what ever reason my $selection value is always empty. The most I have ever gotten to print was the word "on" which would only be on if at least one of the checkboxes were checked off. I have attempted over 30 different solutions to iterating through checkbox arrays, but nothing seems to work. What am I doing wrong?
Sorry for the poorly formatted code, and thanks in advance for the help.
*/
try adding the value=
attribute to your checkboxes
<tr><td>
<input type="checkbox" name="toppings[]" value="Pepperoni" />Pepperoni<br>
<input type="checkbox" name="toppings[]" value="Mushroom"/>Mushroom<br>
<input type="checkbox" name="toppings[]" value="Bacon" />Bacon
</td>
<td>
<input type="checkbox" name="toppings[]" value="Onion" />Onion<br>
<input type="checkbox" name="toppings[]" value="Tomatoes" />Tomatoes<br>
<input type="checkbox" name="toppings[]" value="Olives" />Olives
</td>
<td>
<input type="checkbox" name="toppings[]" value="Pinapple" />Pineapple<br>
<input type="checkbox" name="toppings[]" value="Green Pepper" />Green Pepper<br>
<input type="checkbox" name="toppings[]" value="Extra Cheese" />Extra Cheese
</td></tr>
and check $_POST["toppings"]
, not $_POST["toppings[]"]
$toppings = isset($_POST["toppings"]) ? $_POST["toppings"] : array();
if(!empty($toppings)){
foreach($toppings as $chk => $selection){
echo $selection . ", ";
}