Question : PHP Quiz

Hello,

I have a quiz that I put together using HTML (POST Form) and PHP. The quiz will provide a score after the visitor click submit.  What I am tryin to figure out is the best way to show the visitor the question with the answer they choose and the correct answer(if it is not the answer they choose) on my "results" page.

Answer : PHP Quiz

Use the second suggestion, or use PHP for the whole thing!

Read out the form answers with PHP, and have the list with correct answers already waiting in the PHP-part on the result page. As said, I'd suggest the use of an array and walk thru it step-by-step, but you could have every correct answer waiting in a separate variable.

Forgive my errors, my php is quite rusty these days.
====

<?
//set counter
$counter = 0;

//set list of correct answers
$correct1 = "A";
$correct2 = "B";

//check if form data is posted
if(isset($_POST))
{
//take out the list of the form, don't forget to sanitize user input!
$answer1 = htmlsafechars($_POST['answer1']);
$answer2 = htmlsafechars($_POST['answer2']);

echo "QUESTION 1<br />";
if($answer1 == $correct1)
{
echo "Your answer of " . $answer1 . " was correct!<br /><br />";
$counter++;
}
else
{
echo "Your answer: " . $answer1 . "<br />";
echo "But the correct answer was: " . $correct1 . "<br /><br />";
}

echo "QUESTION 2<br />";
if($answer2 == $correct2)
{
echo "Your answer of " . $answer2 . " was correct!<br /><br />";
$counter++;
}
else
{
echo "Your answer: " . $answer2 . "<br />";
echo "But the correct answer was: " . $correct2 . "<br /><br />";
}

echo "You scored " . $counter . " correct answers!";
}

?>

==
I hope this clears the picture a bit, but again, I'd use arrays.
Random Solutions  
 
programming4us programming4us