1

I'm currently making a quiz with html and PHP, and I'd like to use checkboxes where there are multiple correct answers. The user must get ALL correct answers to get one mark per question. I've been looking at lots of tutorials but nothing seems to quite have what I need. Any help would be massively appreciated!

Here's an example of one of the questions, if you could tell me what is needed adding:

<p class="question">2. Which clubs did Arsene Wenger manage before taking the arsenal job in 1996?</p>
<ul class="answers">
    <input type="checkbox" name="q2[]" id="q2a" value="q2a"><label for="q2a" class="labela">Monaco</label><br/>
    <input type="checkbox" name="q2[]" id="q2b" value="q2b"><label for="q2b" class="labelb">Paris Saint-Germain</label><br/>
    <input type="checkbox" name="q2[]" id="q2c" value="q2c"><label for="q2c" class="labelc">Bayern Munich</label><br/>
    <input type="checkbox" name="q2[]" id="q2d" value="q2d"><label for="q2d" class="labeld">Nagoya Grampus</label><br/>
</ul>

and on a seperate answers page what i have at the moment:

<?php
$q2 = $_POST['q2'];

    $total = 0;

    if ($q2 == "a") { $total = $total + 0.5; }
    if ($q2 == "b") { $total = $total + 0; }
    if ($q2 == "c") { $total = $total + 0; }
    if ($q2 == "d") { $total = $total + 0.5; }
?>
xbakesx
  • 13,202
  • 6
  • 48
  • 76
Josh Strachan
  • 39
  • 1
  • 8

2 Answers2

2

You can do something similar to the following:

<?php
//This should contain four array elements 0-3
$q2 = $_POST['q2'];

$total = 0;

foreach($q2 as $key=>$a2)
{
   //Check if this is the first or fourth checkbox and that it is ticked
   if(($key == 0 && $a2) || ($key == 3 && $a2)
   { 
      //We have checkbox 1 or 4 and they have been checked
      $total = $total + 0.5;
   }
}
?>

The only thing is that you will have to loop through each question and change the loop each time so that you are only selecting the right answers. Ie the line below:

 if(($key == 0 && $a2) || ($key == 3 && $a2)

For question three the right answer may be checkbox 1 and 2, so therefore the script would start to look like so

<?php
$q2 = $_POST['q2'];
$q3 = $_POST['q3'];

$total = 0;

foreach($q2 as $key=>$a2)
{
   //Check if this is the first or fourth checkbox and that it is ticked
   if(($key == 0 && $a) || ($key == 3 && $a)
   { 
      //We have checkbox 1 or 4 and they have been checked
      $total = $total + 0.5;
   }
}

foreach($q3 as $key=>$a3)
{
   //Check if this is the first or second checkbox and that it is ticked
   if(($key == 0 && $a3) || ($key == 1 && $a3)
   { 
      //We have checkbox 1 or 2 and they have been checked
      $total = $total + 0.5;
   }
}
?>

This means you have some work to do if it is a 20 question quiz. Also note that if they check a box that is not the right answer they will not lose any points. Therefore if I ticked every checkbox on the page, I would get a 100% score, even though potentially I checked a box with an incorrect answer, to solve this you could minus if a wrong box is checked, like so:

foreach($q3 as $key=>$a3)
{
   //Check if this is the first or second checkbox and that it is ticked
   if(($key == 0 && $a3) || ($key == 3 && $a3)
   { 
      //We have checkbox 1 or 2 and they have been checked
      $total = $total + 0.5;
   }
   else
  {
     $total = $total - 0.5;
  }
}
The Humble Rat
  • 4,586
  • 6
  • 39
  • 73
  • Thanks a lot, this was a great answer. Thanks for going the extra mile to help :) – Josh Strachan May 05 '15 at 12:54
  • @JoshStrachan my pleasure. Good luck with the coding! – The Humble Rat May 05 '15 at 12:59
  • Hey, im almost completely done with my code, but i was wondering if you could quickly help me or point me in the right direction with a couple other things? I was wondering if there's a way to disable the submit button until all forms have been answered? And secondly my quiz is over 2 pages, how would i move to the next page and keep the answers saved so i can submit at the end of the second page – Josh Strachan May 05 '15 at 13:30
  • To disable the submit button you will need something similar to so: http://stackoverflow.com/questions/16761696/disable-submit-button-until-input-fields-filled-in You will need to modify this so that you check each field, but at the same time be careful say that you don't require everything checked otherwise the user will not be able to continue. Something like this may also help: http://stackoverflow.com/questions/2684434/jquery-check-if-at-least-one-checkbox-is-checked. – The Humble Rat May 05 '15 at 13:49
  • To have your form over multiple pages you will need to have say two pages and a form on each, the first page has say ten questions, when you submit this, it posts it to the second page, the second page ensures results, stores them in a hidden field which is subsequently submitted when submitting the second form ie the next 10 questions. Another possibility is to use sessions, see here: http://www.html-form-guide.com/php-form/php-order-form.html Both request however, may require a new question. One bad thing with coding, ideas take seconds, implementation is another kettle of fish entirely. – The Humble Rat May 05 '15 at 13:51
  • @JoshStrachan hopefully the above will put you on the right track. – The Humble Rat May 05 '15 at 13:54
0

If element name contains [], that is an array, you must check if value is in array with in_array()

if (in_array("a",$q2)) { $total = $total + 0.5; }
Skriptotajs
  • 844
  • 6
  • 15