Need help!!! Please

I am new to PHP so please bear with me.
I have a page that I display several questions retrieving from the DB along with its answers. I do not have any problems displayed them. Each question have 4 checkboxes. Users can check as many boxes as it applied. After users submit the form, I need to compare their answers with the correct answers for each question. How do I get their answer from each question to compare. I used $_POST to get their answers, but i get an array of all the checkboxes which does not help me to compare for each answer.

Here is my code:

<input type='checkbox' name='option[]' value='a'>
<input type='checkbox' name='option[]' value='b'>
<input type='checkbox' name='option[]' value='c'>
<input type='checkbox' name='option[]' value='d'>

each answer is a checkbox. User can check as many checkboxes as they want.

1 .question1......?
a. answer1
b. answer2
c. answer3
d. answer4

User chose d and a
Correct answer is a and b

2 .question2......
a. answer1
b. answer2
c. answer3
d. answer4

User chose a and b.
Correct answer is c.

$boxes=$_POST['option']; 
echo("<p>You selected answer(s): "); 
foreach ($boxes as $key) {echo "Key: $key";}

The correct answer of each question is hidden from user.

thank you

Recommended Answers

All 2 Replies

I would use array_diff.

The way you have your form setup, you should get an array of answers. Its really the easiest way to get data from a checkbox.

Try:

Your correct answers need to be in an array though, which isn't hard.

<?php

$options = $_POST['option'];
if ( count( array_diff( $answer,$options ) ) !== 0 ) {
  //incorrect answer
}
?>

Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.