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; }
?>
2
  • your inputs are arrays, so your PHP needs to treat them as an array; you're not doing that. Commented May 5, 2015 at 12:27
  • Sorry, im literally completely new to php (as of yesterday), so could you explain a bit more? thanks for answering so quickly :) Commented May 5, 2015 at 12:29

2 Answers 2

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;
  }
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks a lot, this was a great answer. Thanks for going the extra mile to help :)
@JoshStrachan my pleasure. Good luck with the coding!
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
To disable the submit button you will need something similar to so: stackoverflow.com/questions/16761696/… 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: stackoverflow.com/questions/2684434/….
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: 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.
|
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; }

1 Comment

Thank you so much! Knew it would be something simple

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.