php 循环中的单选按钮不起作用

Radio Button in a php loop not working

我正在尝试处理从表单中捕获的信息,但它不起作用。我没有从表格中获得任何信息。

我有以下表单代码:

<form class="customform" action="" method="post">

    <table class="table table-bordered table-striped">

        <tbody>

            <?php foreach($question_row->results() as $test)  { ?>
                <tr>
                    <td><?php echo $test->question?></td>
            <td width="30%">
                <input type="radio" name="question[<?php echo $test->id ?>]" value="<?php echo $test->categoryId?>"> Yes
                <input type="radio" name="question[<?php echo $test->id?>]" value="0"> No
            </td>
            </tr>

      <?php   } ?>

        </tbody>
    </table>
    <input type="hidden" name="token" value="<?php echo Token::generate()?>">
    <div class="s-4"><button type="submit">Submit</button></div>


</form>

以下是我处理表格的方式:

<?php

require_once 'core/init.php';

$question = new Question();

$question_row = DB::getInstance()->query("SELECT * FROM questions");


if(Input::exists()) {
if (Token::check(Input::get('token'))) {

    $user = new User();

   try{
         $_SESSION['food'] = Input::get('question[1]');
         Redirect::to('test');

     }catch (Exception $e)
     {
         die($e->getMessage());
     }

}
}

我用 firebug 检查了表单输出,一切都是正确的。现在,当我尝试处理已单击提交按钮的表单时,我没有收到错误消息,来自 Input::get('question[1]') 的信息未传递到会话变量。我将 Input::get('token') 分配给会话变量并且它起作用了。我在这条线上做错了什么吗:

<td width="30%">
                <input type="radio" name="question[<?php echo $test->id ?>]" value="<?php echo $test->categoryId?>"> Yes
                <input type="radio" name="question[<?php echo $test->id?>]" value="0"> No
            </td>

我已经看了好几个小时了

您正在创建具有索引的元素数组,并且您正在检查 post 具有静态索引值的变量。最好创建简单的元素数组,如

<input type="radio" name="question<?php echo $test->id ?>" value="<?php echo $test->categoryId?>"> Yes
<input type="radio" name="question<?php echo $test->id?>" value="0"> No

并使用

等设置条件验证 post 值
isset($_POST["question".$test->id])?"":"";