我正在尝试制作一个简单的php测验应用程序。我试图显示所有答案不同的问题。问题是,当我单击第一个问题时,单选按钮被选中,但是当我选择第二个问题时,单选按钮被选中,但是第一个问题中的一个消失了。我想制作一种表格,允许用户从每个问题中选择一个答案,然后将数据传递到test_calc.php文件进行处理。这是代码:
<?php
$connection = mysqli_connect("localhost", "root", "", "vartvald");
if (isset($_GET['id'])) {
$fk = mysqli_real_escape_string($connection, $_GET['id']);
if ($connection->connect_error) {
die("Connection failed:" . $connection->connect_error);
} else {
$sql = "SELECT * FROM questions WHERE fk_test='$fk'";
}
$result = $connection->query($sql);
if ($result->num_rows > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$id = $row['id'];
$question = $row['question'];
$mark = $row['mark'];
$answer_1 = $row['answer_1'];
$answer_2 = $row['answer_2'];
$answer_3 = $row['answer_3'];
$answer_4 = $row['answer_4'];
$correct = $row['correct'];
echo "<div><form method='POST' action='test_calc.php?id=".$fk."'><br><b>" . $row['question'] . "</b><br><input type='radio' name='answer[]' value=".$answer_1.">"
. $row['answer_1'] . "<br><input type='radio' name='answer[]' value=".$answer_2.">" . $row['answer_2'] . "<br><input type='radio' name='answer[]' value=".$answer_3.">" . $row['answer_3'] .
"<br><input type='radio' name='answer[]' value=".$answer_4.">" . $row['answer_4'] . "<input type='hidden' name='mark' value=".$row['mark'].">
<input type='hidden' name='correct[]' value=".$row['correct'].">
<input type='hidden' name='id' value=".$row['id']."><br><br></div>";
}
echo '<button type="submit"><b>Baigti testa</b></button>';
} else {
echo "</table><h2 style='text-align:center'>Testas neturi sukurtu klausimu..</h2>";
}
}
答案 0 :(得分:1)
所有单选按钮均在同一组中:answer[]
每个问题都需要一个单独的名称,例如:
echo "... <input type='radio' name='answer_".$id."[]' value=".$answer_4."> ..."
答案 1 :(得分:-1)
为循环中的每个问题设置唯一的名称