【发布时间】:2015-03-06 14:26:41
【问题描述】:
我有一个严格使用 PHP 制作的测验表格。有五组三个单选按钮。 (测验中的每个问题三个。)一切正常,但是在单击提交按钮或刷新页面时,选中的单选按钮总是在每组中恢复为仅选项 3,无论用户最初是否选择了选项 1或者 2. 我怎样才能让我的 php 在用户点击提交他们原来的选择时保持选中状态?
$quiz = array();
$quiz['questions'] = array(
"Which of these volcanic rocks are capable of floating?",
"What gemstone is Arkansas particularly known for?",
"What gemstone is assossiated with he month of March?",
"Which gemstone is an alternative birthstone for June?",
"In mythology, which of these gemstones are thought to strengthen and clense one's mind?");
$quiz['choices'][0] = array("Basalt", "Obsidian", "Pumice");
$quiz['choices'][1] = array("Diamond", "Amythist", "Arkanite");
$quiz['choices'][2] = array("Emerald", "Aquamarine", "Ruby");
$quiz['choices'][3] = array("Obsidian", "Bloodstone", "Alexandrite");
$quiz['choices'][4] = array("Emerald", "Turquoise", "Amythist");
//Answers
$quiz['answers'] = array('Pumice', 'Diamond', 'Aquamarine', 'Alexandrite', 'Emerald', 'submit');
foreach ($quiz['questions'] as $key => $question){
echo "<h3>" . $question . "</h3>";
foreach($quiz['choices'][$key] as $choice){
echo "<label>";
if(isset($_POST[$key])){
echo "<input type=\"radio\" name=\"$key\" value=\"$choice\" id=\"$choice\" checked> " . $choice . "<br>";
}else{
echo "<input type=\"radio\" name=\"$key\" value=\"$choice\" id=\"$choice\"> " . $choice . "<br>";
}
echo "</label>";
}}
【问题讨论】:
标签: php forms radio-button