$host = 'localhost';
$user = 'root';
$pw = '';
$db = 'pmdb';
mysql_connect($host,$user,$pw);
mysql_select_db($db);
$result = mysql_query("SELECT * FROM Questions WHERE QuizID=1");
$num_rows = mysql_num_rows($result);
while($row = mysql_fetch_assoc($result))
{
$array[] = $row;
}
for($i=0; $i<=($num_rows-1); $i++)
{
$title = $array[$i]['Title'];
$ans1 = $array[$i]['Answer1'];
$ans2 = $array[$i]['Answer2'];
$ans3 = $array[$i]['Answer3'];
$ans4 = $array[$i]['Answer4'];
echo $title.'<br>';
echo '<form method="post">';
echo '<input type="radio" name="ans'.$i.'">'.$ans1.'<br>';
echo '<input type="radio" name="ans'.$i.'">'.$ans2.'<br>';
echo '<input type="radio" name="ans'.$i.'">'.$ans3.'<br>';
echo '<input type="radio" name="ans'.$i.'">'.$ans4.'<br>';
}
echo '<input type="submit" value="submit" id="submit">';
echo '</form>';
我设法显示问题后跟相应的选项,并在底部显示提交按钮。
如果单击“提交”按钮,我将如何获取用户为从数据库中循环出的每个问题选择的值? ans1
,ans2
等
这是比较他们对答案键的答案并计算他们的分数所必需的。
请帮忙!非常感谢你,更有力量!答案 0 :(得分:1)
为每个无线电输入分配一个值,更新的脚本可以是:
$host = 'localhost';
$user = 'root';
$pw = '';
$db = 'pmdb';
mysql_connect($host,$user,$pw);
mysql_select_db($db);
$result = mysql_query("SELECT * FROM Questions WHERE QuizID=1");
$num_rows = mysql_num_rows($result);
while($row = mysql_fetch_assoc($result))
{
$array[] = $row;
}
//Start the form
echo "<form method=\"post\" action=\"path/to/receiving.php\">\n";
for($i=0; $i<=($num_rows-1); $i++)
{
//Render a question + the answer choices
echo $array[$i]['Title']."<br />\n";
for ($j=1;$j<=4;$j++) {
echo "<input type=\"radio\" name=\"ans$i\" value=\"$j\">".
$array[$i]['Answer'.$j]."<br />\n";
}
}
//End the form
echo "<input type=\"submit\" value=\"submit\" id=\"submit\">\n</form>";
现在从PHP中读取答案的值:
echo $_POST["ans1"];
//The answer given for question 1, will be between
// 1-4 or null (if they didn't answer