我有一个文本文件,我用它来创建一个测试。然而,我一次只显示一个小测验问题。我根据问题的数量生成单选按钮,然后在生成单选按钮的循环之后有一个提交按钮。当我单击submit时,我不知道如何获得用户在$_POST数组中选择的单选按钮。我最初的想法是使用一个表单标记,然后让循环运行,但我不知道这是否有效,也不知道如何使它在语法上工作。
textfile (最后一个数字是正确答案的索引):
What does charmander evolve to?#Charmeleon:charizard:squirtle#0
WHo is the main character in Pokemon?#Misty:Ash:Obama#1
剧本:
<?php
$indexTemp = intVal($_SESSION["index"]);
if($_SESSION["numQuestions"] == $indexTemp){
echo "Your Results are: ";
echo "<form action=\"process.php\" method=\"post\"> Back to Main screen: <input type=\"submit\"><br \> </form>";
exit();
}
$filename = $_SESSION["quizOfTheDay"];
$quizStuff = file($filename);
$ctr =1;
$questionInfo = $quizStuff[$indexTemp];
$questionParse = explode("#", $questionInfo);
$_SESSION["correctAns"] = $questionParse[2];
echo $_SESSION["correctAns"]." from line 55 <br />";
$answerChoices = explode(":",$questionParse[1]);
echo "$questionParse[0] ? <br />";
#This is where the radio buttons are being generated
foreach ($answerChoices as $answerChoice) {
echo "<input type='radio' name='ans$ctr' id='q1' value='$ctr'> <label for='q1'> $answerChoice </label> <br />";
$ctr +=1;
}
$_SESSION["index"] = $indexTemp +1;
echo "<form action=\"questions.php\" method=\"post\"> <input type=\"submit\"><br \> </form>";
?>
发布于 2016-09-15 20:30:17
从单选按钮和复选框中获取数据可能有点棘手,通常是因为对单选按钮和复选框的工作方式缺乏了解。
重要的是要记住两个事实:
更新foreach,因为所有单选按钮都必须有same name
,但值不同。
<?php
foreach ($answerChoices as $answerChoice) {
echo "<input type='radio' name='ans' id='q1' value=".$ctr."> <label for='q1'>".$answerChoice."</label> <br />";
$ctr +=1;
}
?>
现在,您的for-每个连接看起来也是错误的,我已经更新了它,并且由于连接错误,值将不会显示在单选按钮中。单选按钮值将是增量变量的计数。
发布于 2016-09-15 20:36:14
删除$ctr
foreach ($answerChoices as $answerChoice) {
echo "<input type='radio' name='ans' id='q1' value='$ctr'> <label for='q1'> $answerChoice </label> <br />";
$ctr +=1;
}
https://stackoverflow.com/questions/39523467
复制相似问题