添加单选按钮

时间:2012-10-05 08:18:05

标签: php mysql radio-button

我的电脑老师要我做一个简单的php mySql数据库多项选择测验,但现在我卡住了。我做了input-form.php。因此,当他进入页面时,他可以输入问题和多项选择答案。到目前为止,我的作品是input-form.php和quiz1.php,它在数据库中显示了什么。我现在需要有关如何在多项选择答案旁边使用单选按钮的帮助,以便学生可以阅读问题并单击正确的答案。在他们阅读并点击那里选择之后,当他们点击提交时,它将向教师发送测试的电子邮件,其中包含问题和多项选择答案以及他们选择的答案。

测试:

What process determines the identity of a user? 
A. Authentication 
B. tt4ert4t 
C. 4tt4t 
D. 4tt4 

Which of the following user account types can create other user accounts? 
A. uyiyuiy 
B. iuyiuiiu 
C. Administrator 
D. uykuykuyikuy 

To which of the following groups does a standard user in Windows 2000 belong by default? (Select two.) 
A. Limited Users 
B. Power Users 
C. Restricted Users 
D. Users 

依旧......

Php代码:

<?php

// connect to your MySQL database here 
require_once "db_connect.php"; 

// Build the sql command string 
$sqlCommand = "SELECT `question`, `a`, `b`, `c`, `d` FROM `quiz1`"; 
// Execute the query here now 
$query = mysql_query($sqlCommand) or die (mysql_error()); 
// Output the data here using a while loop, the loop will return all members 
while ($row = mysql_fetch_array($query)) { 
// Gather all $row values into local variables for easier usage in output
$question = $row["question"];
$a = $row["a"];
$b = $row["b"];
$c = $row["c"];
$d = $row["d"];


// echo the output to browser 
echo "$question
<br />A. $a 
<br />B. $b 
<br />C. $c
<br />D. $d
<br /><hr />"; 
  } 
 // Free the result set if it is large 
 mysql_free_result($query);  
 // close mysql connection 
 mysql_close(); 

 ?>

2 个答案:

答案 0 :(得分:1)

单选按钮按名称分组。第一个答案的例子: 什么过程决定了用户的身份?

<p>What process determines the identitiy of a user?</p>
<ol>
    <li><label><input type="radio" name="q1" value="1">Authentication</label></li>
    <li><label><input type="radio" name="q1" value="2">tt4ert4t</label></li>
    <li><label><input type="radio" name="q1" value="3">4tt4t</label></li>
    <li><label><input type="radio" name="q1" value="4">4tt4</label></li>
</ol>

使用<label>元素可以在单击标签本身时提供免费的免费功能,并选择单选按钮(试一试!)。此外,<ol>元素定义了一个有序列表。


对于有多个选项的问题,应使用<input type="checkbox">代替radio

答案 1 :(得分:0)

您应该使用复选框来获得多个答案:

echo "$question
<br /><input type='checkbox' name='answer[]' value='$a' />A. $a 
<br /><input type='checkbox' name='answer[]' value='$b' />B. $b 
<br /><input type='checkbox' name='answer[]' value='$c' />C. $c
<br /><input type='checkbox' name='answer[]' value='$d' />D. $d
<br /><hr />"; 

或多个问题:

name='answer[$id][]' // where $id is the id of the question.

在下一页上,您将获得一个包含所有选定答案的数组$_POST['answer']