我正在制作一个多项选择题,在第一页上,radiobuttons看起来像这样
$resultx = $connx->query($sqlx);
$qcounter = 1;
echo '<form action="result.php" method="post">';
echo "<table>";
while ($row = mysqli_fetch_array($resultx)) {
echo '<tr><td>Question '.$qcounter.'<br/></td></tr>';
//output a row here
echo "<tr><td>".($row['Question'])."</td></tr>";
echo '<tr><td><input type="radio" name="q' . $qcounter . '[]" value="A" id="question-1-answers-A"/><label for="question-1-answers-C3">A) '.($row['AnswerA']).'</label></td></tr>';
echo '<tr><td><input type="radio" name="q' . $qcounter . '[]" value="B" id="question-1-answers-B"/><label for="question-1-answers-C3">B) '.($row['AnswerB']).'</label></td></tr>';
echo '<tr><td><input type="radio" name="q' . $qcounter . '[]" value="C" id="question-1-answers-C"/><label for="question-1-answers-C3">C) '.($row['AnswerC']).'</label></td></tr>';
echo '<tr><td><input type="radio" name="q' . $qcounter . '[]" value="D" id="question-1-answers-D"/><label for="question-1-answers-C3">D) '.($row['AnswerD']).'</label></td></tr>';
echo '<tr><td><input type="radio" name="q' . $qcounter . '[]" value="E" id="question-1-answers-E"/><label for="question-1-answers-C3">E) '.($row['AnswerE']).'</label></td></tr>';
echo '<tr><td><input type="hidden" name="question-id[]" value='.($row['id']).'</td></tr>';
echo '<tr><td><input type="hidden" name="question_bar_code[]" id="question_bar_code" value='.($row['CorrectAnswer']).'</td></tr>';
echo '<tr><td><br/></td></tr>';
$qcounter++;
}
echo "</table>";
echo '<div>';
echo '<input class="hvr-fade1" type="submit" value="Finish">';
echo '</div>';
echo '</form>';
我有一个计数器(qcounter)来命名radiobuttons,并且每个问题的radiobuttons的名称是q1,q2,q3,q4,因为它们是循环的,具体取决于问题的数量。
现在是我的第二页,我分析结果我有这个代码来检索并查看所选的radiobuttons:
<?php
$qcounter = 1;
$answer1 = $_POST["q".$qcounter];
foreach($answer1 as $item1) {
echo "<table>";
echo "<tr><td>".$item1."</td></tr>";
$qcounter2++;
echo "</table>";
}
?>
但它只回显了选定的q1
单选按钮我能以任何方式让他们全部回应吗?
答案 0 :(得分:3)
您可以在loop
之外设置计数器并在循环内使用它:
$qcounter = 1;
while ($row = mysqli_fetch_array($resultx)) {
echo '<tr><td>Question '.$qcounter.'<br/></td></tr>';
echo "<tr><td>".($row['Question'])."</td></tr>";
echo '<tr><td><input type="radio" name="q' . $qcounter . '[]" value="A" id="question-1-answers-A"/><label for="question-1-answers-C3">A) '.($row['AnswerA']).'</label></td></tr>';
echo '<tr><td><input type="radio" name="q' . $qcounter . '[]" value="B" id="question-1-answers-B"/><label for="question-1-answers-C3">B) '.($row['AnswerB']).'</label></td></tr>';
echo '<tr><td><input type="radio" name="q' . $qcounter . '[]" value="C" id="question-1-answers-C"/><label for="question-1-answers-C3">C) '.($row['AnswerC']).'</label></td></tr>';
echo '<tr><td><input type="radio" name="q' . $qcounter . '[]" value="D" id="question-1-answers-D"/><label for="question-1-answers-C3">D) '.($row['AnswerD']).'</label></td></tr>';
$qcounter++;
}
更改问题后,这是一个更好的答案:
您应该将所有input
更改为多维数组:
$resultx = $connx->query($sqlx);
$qcounter = 1;
echo '<form action="result.php" method="post">';
echo "<table>";
while ($row = mysqli_fetch_array($resultx)) {
echo '<tr><td>Question '.$qcounter.'<br/></td></tr>';
//output a row here
echo "<tr><td>".($row['Question'])."</td></tr>";
echo '<tr><td><input type="radio" name="q[' . $qcounter . '][]" value="A" id="question-1-answers-A"/><label for="question-1-answers-C3">A) '.($row['AnswerA']).'</label></td></tr>';
echo '<tr><td><input type="radio" name="q[' . $qcounter . '][]" value="B" id="question-1-answers-B"/><label for="question-1-answers-C3">B) '.($row['AnswerB']).'</label></td></tr>';
echo '<tr><td><input type="radio" name="q[' . $qcounter . '][]" value="C" id="question-1-answers-C"/><label for="question-1-answers-C3">C) '.($row['AnswerC']).'</label></td></tr>';
echo '<tr><td><input type="radio" name="q[' . $qcounter . '][]" value="D" id="question-1-answers-D"/><label for="question-1-answers-C3">D) '.($row['AnswerD']).'</label></td></tr>';
echo '<tr><td><input type="radio" name="q[' . $qcounter . '][]" value="E" id="question-1-answers-E"/><label for="question-1-answers-C3">E) '.($row['AnswerE']).'</label></td></tr>';
echo '<tr><td><input type="hidden" name="question-id[]" value='.($row['id']).'</td></tr>';
echo '<tr><td><input type="hidden" name="question_bar_code[]" id="question_bar_code" value='.($row['CorrectAnswer']).'</td></tr>';
echo '<tr><td><br/></td></tr>';
$qcounter++;
}
echo "</table>";
echo '<div>';
echo '<input class="hvr-fade1" type="submit" value="Finish">';
echo '</div>';
echo '</form>';
现在 - 在用户post
编辑表单后 - 您应该遍历$_POST['q']
数组以获取相关值:
foreach ($_POST['q'] as $qid => $answer1) {
foreach($answer1 as $item1) {
echo "<table>";
echo "<tr><td>".$item1."</td></tr>";
$qcounter2++;
echo "</table>";
}
}
您最好使用
var_dump($_POST['q']);
甚至var_dump($_POST['q'][1]);
来更好地了解在提交表单后从浏览器收到的数据。