Quiz in PHP
Posted: Sat Mar 01, 2008 1:54 am
I am new to php and so I dont know php very well. I am making a quiz in my website. I have already have the code for this. In the code I have the answers to the quiz can be only one, but there could be more than one answer to a question. So below is the code I have for validating only one answer to the question. I need to modify, so that the results page can
validate for more than one answers.
<FORM action=http://www.********.com/*****questions/quizresult1.php method="POST">
<ul>
<h6><b>1. What is IPOD? (Select Two)</b></h6>
<ul>
<INPUT TYPE="checkbox" NAME="question1" VALUE="answer1.1"> Music Player<BR>
<INPUT TYPE="checkbox" NAME="question1" VALUE="answer1.2"> Apple Product<BR>
<INPUT TYPE="checkbox" NAME="question1" VALUE="answer1.2"> Internet Phone<BR>
</ul>
</ul>
<ul>
<h6><b>2. Who is Steve Jobs?</b></h6>
<ul>
<INPUT TYPE="checkbox" NAME="question2" VALUE="answer2.1"> Founder of Apple Inc<BR>
<INPUT TYPE="checkbox" NAME="question2" VALUE="answer2.2"> Founder of Oracle<BR>
<INPUT TYPE="checkbox" NAME="question2" VALUE="answer2.3"> Dont know<BR>
</ul>
</ul>
<ul>
<INPUT TYPE="SUBMIT" VALUE="Done" >
<INPUT TYPE="RESET" VALUE="Clear all fields of this form">
</ul>
</FORM>
*************************************************************************
RESULT PHP Code:
<?php
$question1 = $_POST['question1'];
$question2 = $_POST['question2'];
?>
<?php
$score = 0; //initialize score to zero
?>
<h6><b>1. What is IPOD?</b></h6>
<p>
<ul>Answer: Music Player, Apple Product<br>
Your answer was:
<?php
if ($question1 == "answer1.1")
{
echo ("<font color = #009900><b>Correct</b></font>");
$score = $score + 1;
}
else
{
echo ("<font color = #FF0000><b>Incorrect</b></font>");
}
?>
</ul>
</p>
<h6><b>2. Who is Steve Jobs?</b></h6>
<p>
<ul>Answer: Founder of Apple Inc<br>
Your answer was:
<?php
if ($question2 == "answer2.1")
{
echo ("<font color = #009900><b>Correct</b></font>");
$score = $score + 1;
}
else
{
echo ("<font color = #FF0000><b>Incorrect</b></font>");
}
?>
</ul>
</p>
So if u observe the results PHP code under Question 1, it should receive two answers and show whether the user marked correct. Please let me know how I should modify my code.
Thanks.
Sidhu
validate for more than one answers.
<FORM action=http://www.********.com/*****questions/quizresult1.php method="POST">
<ul>
<h6><b>1. What is IPOD? (Select Two)</b></h6>
<ul>
<INPUT TYPE="checkbox" NAME="question1" VALUE="answer1.1"> Music Player<BR>
<INPUT TYPE="checkbox" NAME="question1" VALUE="answer1.2"> Apple Product<BR>
<INPUT TYPE="checkbox" NAME="question1" VALUE="answer1.2"> Internet Phone<BR>
</ul>
</ul>
<ul>
<h6><b>2. Who is Steve Jobs?</b></h6>
<ul>
<INPUT TYPE="checkbox" NAME="question2" VALUE="answer2.1"> Founder of Apple Inc<BR>
<INPUT TYPE="checkbox" NAME="question2" VALUE="answer2.2"> Founder of Oracle<BR>
<INPUT TYPE="checkbox" NAME="question2" VALUE="answer2.3"> Dont know<BR>
</ul>
</ul>
<ul>
<INPUT TYPE="SUBMIT" VALUE="Done" >
<INPUT TYPE="RESET" VALUE="Clear all fields of this form">
</ul>
</FORM>
*************************************************************************
RESULT PHP Code:
<?php
$question1 = $_POST['question1'];
$question2 = $_POST['question2'];
?>
<?php
$score = 0; //initialize score to zero
?>
<h6><b>1. What is IPOD?</b></h6>
<p>
<ul>Answer: Music Player, Apple Product<br>
Your answer was:
<?php
if ($question1 == "answer1.1")
{
echo ("<font color = #009900><b>Correct</b></font>");
$score = $score + 1;
}
else
{
echo ("<font color = #FF0000><b>Incorrect</b></font>");
}
?>
</ul>
</p>
<h6><b>2. Who is Steve Jobs?</b></h6>
<p>
<ul>Answer: Founder of Apple Inc<br>
Your answer was:
<?php
if ($question2 == "answer2.1")
{
echo ("<font color = #009900><b>Correct</b></font>");
$score = $score + 1;
}
else
{
echo ("<font color = #FF0000><b>Incorrect</b></font>");
}
?>
</ul>
</p>
So if u observe the results PHP code under Question 1, it should receive two answers and show whether the user marked correct. Please let me know how I should modify my code.
Thanks.
Sidhu