PHP复选框表单检索,尝试了所有的stackoverflow解决方案


PHP checkbox form retrieval, tried all stackoverflow solutions

<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 . ", ";
}

/*
不管什么原因,我的$selection值总是空的。我打印过的最多的是"on"这个词,它只有在至少一个复选框被选中时才会显示为on。我已经尝试了30多种不同的解决方案来迭代复选框数组,但似乎没有任何工作。我做错了什么?

Sorry for the poorly formatted code, and thanks in advance for the help.

*/

尝试在复选框中添加value=属性

<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>

检查$_POST["toppings"],不检查$_POST["toppings[]"]

$toppings = isset($_POST["toppings"]) ? $_POST["toppings"] : array();
if(!empty($toppings)){
   foreach($toppings as $chk => $selection){
        echo $selection . ", ";
   }