0

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. ;)

  • upvoted for "mystified"... bc i've been there. not with checkboxes but with a lot of things in life... – gloomy.penguin Jan 18 '15 at 03:26
  • Just to be clear.... you're checking these answers after a form submit, right? Try doing a `var_dump($_POST)` to see what checkboxes turn into when you submit them. It's a little different. [This](http://stackoverflow.com/a/18424178/623952) explains it a little. Or... [this](http://stackoverflow.com/questions/18623591/updating-sql-database-with-checkbox-values/18641541#18641541) is a more complicated example I wrote for someone long ago. – gloomy.penguin Jan 18 '15 at 03:31

4 Answers4

0

Because the question is the "select all the correct answers" type, it's not as easy as just checking one variable to see if it is correct. You need to check that the right answers are checked and the wrong answers are NOT checked.

There might be a more elegant way to do it, and you can implement this logic into your code however you see fit, but here is the basic idea you will need:

// Check that the 2 correct answers are set, and the 3 incorrect answers are not set
if (isset($_POST['q10-A'], $_POST['q10-B']) &&
    !isset($_POST['q10-C']) && 
    !isset($_POST['q10-D']) && 
    !isset($_POST['q10-E'])) 
{
    // Correct!
}

See demo

Mark Miller
  • 7,442
  • 2
  • 16
  • 22
  • That works great, especially the demo, which includes the "else if" clause. Now I'll just have to try and figure out a way to plug that into my answer key. –  Jan 18 '15 at 04:12
0

You can use array based system. In HTML part you use the same name with a [] in front of it like this:

<label for="q10-A">
  <input type="checkbox" name="q10[]" id="q10-A" value="A">
  planet</label>
<label for="q10-B">
  <input type="checkbox" name="q10[]" id="q10-B" value="B">
  asteroid</label>
<label for="q10-C">
  <input type="checkbox" name="q10[]" id="q10-C" value="C">
  black hole</label>
<label for="q10-D">
  <input type="checkbox" name="q10[]" id="q10-D" value="D">
  neutrino star</label>
<label for="q10-E">
  <input type="checkbox" name="q10[]" id="q10-E" value="E">
  quasar</label>

then in php part you use a foreach loop to get user input like this:

<?php
$a10=$_POST['q10'];  //put user inputs into a variable called $a10;
//then check what are values of $a10
foreach($a10 as $a) {
   $answer.=$a;  //put checked values in a string for example if user check A and C and D the $answer will be like 'ACD'
}
if($answer=='AB'){ $totalCorrect++; }
?>
NimaNr
  • 532
  • 5
  • 20
0

You can try this, too... with the checkboxes as an array. This is complete code and the form will submit to itself...

<?php

// var_dump($_POST); 

if (!empty($_POST)) {

   $correct_answers=0;

   if (isset($_POST['q10']) &&  
          in_array('A',$_POST['q10']) &&  in_array('B',$_POST['q10']) &&
         !in_array('C',$_POST['q10']) && !in_array('D',$_POST['q10']) ) {
      $correct_answers++;  
   }
   print "$correct_answers correct answers<br/><br/>";
}
// else nothing was submitted

?>

<form method="post" action="">

<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[]" id="q10-A" value="A">
      planet</label>
    <label for="q10-B">
      <input type="checkbox" name="q10[]" id="q10-B" value="B">
      asteroid</label>
    <label for="q10-C">
      <input type="checkbox" name="q10[]" id="q10-C" value="C">
      black hole</label>
    <label for="q10-D">
      <input type="checkbox" name="q10[]" id="q10-D" value="D">
      neutrino star</label>
    <label for="q10-E">
      <input type="checkbox" name="q10[]" id="q10-E" value="E">
      quasar</label>
   </div>
</li>


<button type="submit">Submit</button>
</form>
gloomy.penguin
  • 5,833
  • 6
  • 33
  • 59
0

You can treat checkboxes and radio buttons the same way in your html. Just realize that a radio button group will have zero or one answers, and a checkbox group will have zero or more answers.

In this example I set up an array of questions to correct answers, and if an incoming answer is a checkbox group it gets turned into a flattened string for easy comparison with correct answer.

<body>
<?php   
if (isset($_REQUEST['subbtn'])) {

    // array of questions to alpha-sorted answers
    // "q99" => "BA" will not work, it must be => "AB"
    $questions = array(
        "q9" => "B",
        "q10" => "AB"
    );

    foreach ($questions as $q => $a) {
        if (isset($_REQUEST[$q])) {
            $ga = $_REQUEST[$q];
            if (is_array($ga)) {
              sort($ga);
              $ga = strtoupper(implode("", $ga));
            }
            if ($ga == $a) {
                echo "question " . $q . " is correct<br>";
            } else {
                echo "question " . $q . " is incorrect<br>";
            }
        } else {
            echo "question " . $q . " was not answered<br>";
        }
    }


} else {
?>  
    <form>
        <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">
      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">
      B. expanding</div>
    </label>
  </div>
</li>

<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[]" id="q10-A" value="A">
      planet</label>
    <label for="q10-B">
      <input type="checkbox" name="q10[]" id="q10-B" value="B">
      asteroid</label>
    <label for="q10-C">
      <input type="checkbox" name="q10[]" id="q10-C" value="C">
      black hole</label>
    <label for="q10-D">
      <input type="checkbox" name="q10[]" id="q10-D" value="D">
      neutrino star</label>
    <label for="q10-E">
      <input type="checkbox" name="q10[]" id="q10-E" value="E">
      quasar</label>
   </div>
</li>
<input type='submit' value='Submit' name='subbtn'>

    </form>
<?php
}
?>

</body>
James
  • 20,957
  • 5
  • 26
  • 41