我正在尝试处理从表单捕获的信息,但它不起作用。我没有从表格中得到任何信息。
我有以下表格代码:
<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
并使用类似的isset条件验证后值
isset($_POST["question".$test->id])?"":"";