There is a php test.
I do this: by default, the php handler has two variables - $ ot and $ not - right and wrong answers, initially set them to 0, I raise $ ok by 1 for the right answer, and I raise by $ not for the wrong answer.
code example:

<?php $ot = 0; $not = 0; if ($_POST[q1] == a){$ot++;} else {$not++;} if ($_POST[q2] == a){$ot++;} else {$not++;} ?> 

There are no problems with radio buttons, only one option. But in questions with the checkbox I can not verify the correctness of the selected answers.
Tell me how to implement this in my example.


Here is the html code

 <div> <p>1. ВОПРОС 1...</p> <input name="q1[]" type="checkbox" value="a"> 1</label><br> <input name="q1[]" type="checkbox" value="b"> 2</label><br> <input name="q1[]" type="checkbox" value="c"> 3</label><br> </div> 

And here is a part of my php code

 <?php $ot = 0; $not = 0; $q1 = POST_['q1']; foreach($q1 as $value) { .... } ?> 

That's what to put in place ... I can not understand. The correct answer is 1 and 3 checkboxes, for example.

  • If you select multiple it will be an array. Make a withdrawal of $ _POST [here is the name of the checkbox] that will display, then sort through them - Daniel
  • foreach ($ checkboxes as $ value) {// reconcile here} - Daniel
  • In one array store the response standard, in the other answer of the user. Sort the values. Then subtract the user answers from the standard php.net/array_diff and if the diff is empty, then the correct answers have been selected. Then subtract the reference from the answer. If it is empty again, it means that there are no extra points in the answer either. And only then ok ++. - Lexx918
  • look at the answer I proposed, and if it is possible, add it (I cannot understand how to insert the code in the comment, so I wrote it in the answers) - kristofkadavr

1 answer 1

Basic solution design. Can be improved.

 $successCount = 0; $errorsCount = 0; $validAnswers = [ 'q1' => [1, 3], 'q2' => ['London'], ]; foreach($_POST as $questionKey => $userAnswer) { // проверяем, что такой вопрос действительно есть в списке if (!empty($validAnswers[$questionKey])) { // если не чекбоксы, а "радиобаттон", т.е. только 1 правильный вариант - всё-равно приводим к массиву $userAnswer = !is_array($userAnswer) ? [$userAnswer] : $userAnswer; $successCount += array_intersect($userAnswer, $rightAnswers); $errorsCount += array_diff($userAnswer, $rightAnswers) } }