I've put together a quiz, with the questions and answers stored in a database. It works for multiple choice questions, and I figured out how to make it work with a fill-in-the-blank question, too.
But I haven't figured checkboxes out yet. I have one question that offers users a choice of five answers, each associated with a checkbox. To get it right, they have to select two of the checkboxes.
I think my problem is with this line in my answer key:
if ($answer10 == "AB") { $totalCorrect++; }
For a correct score, a user has to choose the first two checkboxes (values A and B). So AB obviously isn't a right answer, and $answer10 == "A,B" doesn't work, either.
So what's the correct way to tell the answer key the correct choices are A + B?
This is the HTML from a multiple-choice question:
<li id="q9">
<div class="Question">Scientists believe the universe is:</div>
<div class="Answer">
<label for="q9-A">
<div class="Radio">
<input type="radio" name="q9" id="q9-A" value="A" style="display: none;">
A. disappearing</div>
</label>
</div>
<div class="Answer">
<label for="q9-B">
<div class="Radio">
<input type="radio" name="q9" id="q9-B" value="B" style="display: none;">
B. expanding</div>
</label>
</div>
</li>
And this is the HTML from my question featuring checkboxes:
<li id="q10">
<div class="Question">Check each item that can be found in our solar system.</div>
<div class="Answer" style="margin-top: 5px; background: #000; color: #fff; text-align: center;">
<label for="q10-A">
<input type="checkbox" name="q10-A" id="q10-A" value="A">
planet</label>
<label for="q10-B">
<input type="checkbox" name="q10-B" id="q10-B" value="B">
asteroid</label>
<label for="q10-C">
<input type="checkbox" name="q10-C" id="q10-C" value="C">
black hole</label>
<label for="q10-D">
<input type="checkbox" name="q10-D" id="q10-D" value="D">
neutrino star</label>
<label for="q10-E">
<input type="checkbox" name="q10-E" id="q10-E" value="E">
quasar</label>
</div>
</li>
And this is my answer key:
$answer1 = $_POST['q1'];
$answer2 = $_POST['q2'];
$answer3 = $_POST['q3'];
$answer4 = $_POST['q4'];
$answer5 = $_POST['q5'];
$answer6 = $_POST['q6'];
$answer7 = $_POST['q7'];
$answer8 = $_POST['q8'];
$answer9 = $_POST['q9'];
$answer10 = $_POST['q10'];
$answer11 = $_POST['q11'];
$totalCorrect = 0;
if ($answer1 == "A") { $totalCorrect++; }
if ($answer2 == "Jupiter") { $totalCorrect++; }
if ($answer3 == "C") { $totalCorrect++; }
if ($answer4 == "D") { $totalCorrect++; }
if ($answer5 == "A") { $totalCorrect++; }
if ($answer6 == "C") { $totalCorrect++; }
if ($answer7 == "C") { $totalCorrect++; }
if ($answer8 == "B") { $totalCorrect++; }
if ($answer9 == "B") { $totalCorrect++; }
if ($answer10 == "AB") { $totalCorrect++; }
if ($answer11) { $totalCorrect++; }
EDIT
Here's how I plugged Mark M's response into my scoring system:
if (isset($_POST))
{
if (isset($_POST['q10-A'], $_POST['q10-B']) &&
!isset($_POST['q10-C']) &&
!isset($_POST['q10-D']) &&
!isset($_POST['q10-E']))
{
$Checkbox = 'A';
}
else
{
$Checkbox = 'B';
}
}
$answer1 = $_POST['q1'];
$answer2 = $_POST['q2'];
$answer3 = $_POST['q3'];
$answer4 = $_POST['q4'];
$answer5 = $_POST['q5'];
$answer6 = $_POST['q6'];
$answer7 = $_POST['q7'];
$answer8 = $_POST['q8'];
$answer9 = $_POST['q9'];
$answer10 = $Checkbox;
The only drawback with this solution is that I have to create a special script for each question that includes checkboxes. So I'll explore the other answers to see if I can find a solution that handles all checkbox questions automatically. Otherwise, this works fine for me. ;)