Quiz in PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sidhumaharaj
Forum Newbie
Posts: 3
Joined: Sat Mar 01, 2008 1:36 am

Quiz in PHP

Post 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
User avatar
hawkenterprises
Forum Commoner
Posts: 54
Joined: Thu Feb 28, 2008 9:56 pm
Location: gresham,oregon
Contact:

Re: Quiz in PHP

Post 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
sidhumaharaj
Forum Newbie
Posts: 3
Joined: Sat Mar 01, 2008 1:36 am

Re: Quiz in PHP

Post 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
sidhumaharaj
Forum Newbie
Posts: 3
Joined: Sat Mar 01, 2008 1:36 am

Re: Quiz in PHP

Post by sidhumaharaj »

Okay never mind this post.
I initialized few more variables and made it work. Thanks.
Post Reply