Reputation: 21
My computer teacher wants me to make a simple php mySql database multiple choice quiz, but now i'm stuck. I have made input-form.php. So when he goes on the page he can input the questions and multiple choice answers. I have so far that works is the input-form.php and quiz1.php which displays whats in the database. I now need help on how to use radio button next to the multiple choice answers so that the students can read the question and click the right answer. After they have read and click on there choice and when they hit submit it will send the teacher a email of the test with the questions and multiple choice answers also with there answer that they picked.
Test:
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
and so on ....
Php Code:
<?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();
?>
Upvotes: 1
Views: 1427
Reputation: 39704
You should use checkboxes for multiple answers:
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 />";
or for multiple questions:
name='answer[$id][]' // where $id is the id of the question.
on next page you will get an array $_POST['answer']
with all selected answers.
Upvotes: 0
Reputation: 174937
Radio buttons are grouped by name. Example for the first answer: What process determines the identity of a user?
<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>
Having the <label>
element gives that nice free functionality of when you click on the label itself, and the radio button is selected (try it!). Also, the <ol>
element defines an ordered list.
For questions with more than one choice, a <input type="checkbox">
should be used instead of radio
.
Upvotes: 1