Page 1 of 1

Quiz in PHP

Posted: Sat Mar 01, 2008 1:54 am
by sidhumaharaj
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">&nbsp;&nbsp;Music Player<BR>
<INPUT TYPE="checkbox" NAME="question1" VALUE="answer1.2">&nbsp;&nbsp;Apple Product<BR>
<INPUT TYPE="checkbox" NAME="question1" VALUE="answer1.2">&nbsp;&nbsp;Internet Phone<BR>
</ul>
</ul>
<ul>
<h6><b>2. Who is Steve Jobs?</b></h6>
<ul>
<INPUT TYPE="checkbox" NAME="question2" VALUE="answer2.1">&nbsp;&nbsp;Founder of Apple Inc<BR>
<INPUT TYPE="checkbox" NAME="question2" VALUE="answer2.2">&nbsp;&nbsp;Founder of Oracle<BR>
<INPUT TYPE="checkbox" NAME="question2" VALUE="answer2.3">&nbsp;&nbsp;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

Re: Quiz in PHP

Posted: Sat Mar 01, 2008 9:59 am
by hawkenterprises
next time wrap your code in *[ php ] * tags

this is the line you would want to modify

Code: Select all

 
if ($question1 == "answer1.1" || $question1 == "answer1.2")
 
 
|| means "either or"
&& means and or both
! means negation or opposite

Re: Quiz in PHP

Posted: Sat Mar 01, 2008 1:46 pm
by sidhumaharaj
Hi Thanks for the reply
I have tried this OR sign and it works. But I actually want AND sign and this doesnt work. I guess that when I intialize the variable *[$question1 = $_POST['question1'];]* in the second code, it is not able to store both answers. So when I put an AND sign, it always fail.

Please help.

Thanks,
Sidhu

Re: Quiz in PHP

Posted: Sat Mar 01, 2008 3:12 pm
by sidhumaharaj
Okay never mind this post.
I initialized few more variables and made it work. Thanks.