单选按钮在PHP循环不工作

时间:2015-11-22 19:13:56

标签: php loops radio-button

我正在尝试处理从表单中捕获的信息,但它无法正常工作。我没有从表格中获得任何信息。

我有以下代码:

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

我已经看了几个小时

1 个答案:

答案 0 :(得分:0)

您正在使用索引创建元素数组,并且您正在使用静态索引值检查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

并使用isset条件验证post值,如

isset($_POST["question".$test->id])?"":"";