只有一个单选按钮可以用PHP选择,而不是表单
问题描述:
我一直在为这个项目开发这个项目,我们在这个项目中制作了一个迷你课程网站,用来测试用户对我们选择的主题的看法。问题,选择和答案都必须在一个数组中,然后可以显示为问题和单选按钮选择。我到了那个时候,我现在只在一个工作,直到完成它。我现在的问题是所有的单选按钮都是可选的。我怎么能这样做,以便每个问题只能选择一个单选按钮?只有一个单选按钮可以用PHP选择,而不是表单
这里是我的代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
$score = 0;
//multi-dimensional arrays for questions, selections, and answers
$questions = array(
array('question' => 'Which is optional to making Easy Fudge?',
'choice 1' => 'Nuts',
'choice 2' => 'Condensed Milk',
'choice 3' => 'semi-sweet morsels',
'choice 4' => 'bakers chocolate',
'answer' => 'nuts'),
);
//looping through the questions with nested loops
foreach($questions as $question){
echo $question['question'] . '<br />';
echo '<input type="radio" name = "nuts" value="nuts" id="nuts"> ' . $question['choice 1'] . '<br />';
echo '<input type="radio" name = "condensed milk" value="condensed milk" id="condensed milk"> ' . $question['choice 2'] . '<br />';
echo '<input type="radio" name = "semi-sweet morsels" value="semi-sweet morsels" id="semi-sweet morsels"> ' . $question['choice 3'] . '<br />';
echo '<input type="radio" name = "bakers chocolate" value="bakers chocolate" id="backers chocolate"> ' . $question['choice 4'] . '<br />';
}
?>
</body>
</html>
答
给所有相关的单选按钮的名称相同。例如:
foreach($questions as $question){
echo $question['question'] . '<br />';
echo '<input type="radio" name = "nuts" value="nuts" id="nuts"> ' . $question['choice 1'] . '<br />';
echo '<input type="radio" name = "nuts" value="condensed milk" id="condensed_milk"> ' . $question['choice 2'] . '<br />';
echo '<input type="radio" name = "nuts" value="semi-sweet morsels" id="semi-sweet_morsels"> ' . $question['choice 3'] . '<br />';
echo '<input type="radio" name = "nuts" value="bakers chocolate" id="backers_chocolate"> ' . $question['choice 4'] . '<br />';
}
作为再一记..有效身份证属性不能包含空格,所以在上面的例子中,我用下划线代替了你的空间。
单选按钮应共享一个通用名称,但具有不同的值。 – j08691 2013-02-19 22:06:12