douxin8383 2014-03-27 23:18
浏览 25
已采纳

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

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

*/

  • 写回答

1条回答 默认 最新

  • doushi8599 2014-03-27 23:23
    关注

    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 . ", ";
       }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题