PHP Quiz Help

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
FAF101
Forum Newbie
Posts: 3
Joined: Sun Nov 21, 2010 3:42 pm

PHP Quiz Help

Post by FAF101 »

Hello,

I am trying to create an all-in-one form PHP quiz. So far I have this for the form:

<head>
<title>Zodiac Quiz</title>
</head>
<body>
<h1>Chinese Zodiac Quiz!</h1>
<form name="ZodiacQuiz" action="ZodiacQuiz.php" method="get">
<p>1. How many Zodiac Signs are there?</p><input type="radio" name="quest_one" value="13"> 13
<input type="radio" name="quest_one" value="16"> 16
<input type="radio" name="quest_one" value="12">12
<input type="radio" name="quest_one" value="14">14<br/>

<p>2. 1989 is the year of the Snake:</p> <input type="radio" name="quest_two" value="True">True
<input type="radio" name="quest_two" value="False">False<br/>

<p>3. The year of the monkey started in: <input type="text" name="quest_three" value="" /></p><br/>

<p>4. The Cat is in the Chinese Zodiac:</p> <input type="radio" name="quest_four" value="True">True
<input type="radio" name="quest_four" value="False">False<br/>

<p>5. 2010 is the year of the: <input type="text" name="quest_five" value="" /></p><br/>

<p>Please enter your name and e-mail for they are required:</p>
<p>Name <input type="text" name="name" /> Email <input type="text" name="email" /></p>


<input type="submit" value="Submit Quiz"> <input type="reset" value="Reset Quiz">
</form>

That is my form, which I hope I did right and here is what I am working with to try and get the first question to work

Code: Select all

$score = 0;
			$mod = $score % 5;
			if (isset($_REQUEST['quest_one'])) 
			{
				$quest_one = $_REQUEST['quest_one'];
				if($quest_one == 12)
				{
					echo "<p>Correct</p>"; 
					$score++;
				}
				else
				{
					echo "<p>Incorrect. The real answer is 12.</p>";
				}
What I essentially need this to do is to read the users answers and it will tell them after they submit everything if the answers they put in were correct or incorrect and what the real answer is. I also need to to show how many questions they got correct at the end, but right now I am just trying to get the first question to work and I am having some difficulty. Any help would be appreciaited.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP Quiz Help

Post by Celauran »

At a glance, the code looks fine. Can you elaborate on what isn't working? What happens when you submit the form?
FAF101
Forum Newbie
Posts: 3
Joined: Sun Nov 21, 2010 3:42 pm

Re: PHP Quiz Help

Post by FAF101 »

Before I actually submit the quiz in the echos are down at the bottom. Because what i need to do is have it basically, have the user put in the answers. When the quiz is submitted I need it to say either its correct if its correct or if it is wrong that it is wrong and the real answer under the questions and then how many questions they got right at the bottom.
FAF101
Forum Newbie
Posts: 3
Joined: Sun Nov 21, 2010 3:42 pm

Re: PHP Quiz Help

Post by FAF101 »

okay so I have a new way that I am going to try this. i have not tested it yet, but any help would be great. The one thing I believe I may have to change to get it to wait and see if everything is filled in and I think there can be a simpler way of seeing if everything is blank or not. Any help is much appreciated! :D

Form:

Code: Select all

   <head>
        <title>Zodiac Quiz</title>
    </head>
    <body>
        <h1>Chinese Zodiac Quiz!</h1>
		<form name="ZodiacQuiz" action="ZodiacQuizResults.php" method="get">
				<p>1. How many Zodiac Signs are there?</p><input type="radio" name="q1" value="13"> 13
													 <input type="radio" name="q1" value="16"> 16
													 <input type="radio" name="q1" value="12">12
													 <input type="radio" name="q1" value="14">14<br/>
													 
				<p>2. 1989 is the year of the Snake:</p> <input type="radio" name="q2" value="True">True
														 <input type="radio" name="q2" value="False">False<br/>	

				<p>3. The year of the monkey started in: <input type="text" name="q3" value="" /></p><br/>

				<p>4. The Cat is in the Chinese Zodiac:</p> <input type="radio" name="q4" value="True">True
													  <input type="radio" name="q4" value="False">False<br/>
				
				<p>5. 2010 is the year of the: <input type="text" name="q5" value="" /></p><br/>
				
				<p>Please enter your name and e-mail for they are required:</p>
				<p>Name <input type="text" name="name" />   Email <input type="text" name="email" /></p>
				
				
				<input type="submit" value="Submit Quiz"> <input type="reset" value="Reset Quiz">
				<input type="hidden" name="zodquiz" value="ZodiacQuiz.php">

		</form>
	</body>
</html>
(This is ZodiacQuiz.php)

Code: Select all

<head>
        <title>Zodiac Quiz Results</title>
</head>
<body>
        <h1>Chinese Zodiac Results!</h1>

<?php
//Gets the answers from the previous page//
if (isset ($_GET['submit'])) {
   $q1 = $_GET['q1'];
   $q2 = $_GET['q2'];
   $q3 = $_GET['q3'];
   $q4 = $_GET['q4'];
   $q5 = $_GET['q5'];
   $zodquiz = $_GET['zodquiz'];
   $name = $_GET['name'];
   $email = $_GET['email'];
}

//This is to check if question 1 is empty//
if ($q1 == "") 
{
   die ("You forgot something, go back and check over your quiz.");
}

//This is what happens if question 1 is not empty.//
//I think I need to put this in an isset but i am not sure because I dont want anything displayed if there is nothing in the field.//
if ($q1 == 12)
{
	echo "You are Correct!";
	$score++;
}
else
{
	echo "You are wrong. Your answer is $q1  correct answer is 12.";
}


//This is to check if question 2 is empty//
	if ($q2 == "") 
	{
	   die ("You forgot something, go back and check over your quiz.");
	}

//This is what happens if question 2 is not empty.//
//I think I need to put this in an isset but i am not sure because I dont want anything displayed if there is nothing in the field.//	
	if ($q2 == True)
	{
		echo "You are Correct!";
		$score++;
	}
	else
	{
		echo "You are wrong. Your answer is $q2  correct answer is True.";
	}	
	
//This is to check if question 3 is empty//
		if ($q3 == "") 
		{
		   die ("You forgot something, go back and check over your quiz.");
		}
		
//This is what happens if question 3 is not empty.//
//I think I need to put this in an isset but i am not sure because I dont want anything displayed if there is nothing in the field.//
		if ($q3 == 1920)
		{
			echo "You are Correct!";
			$score++;
		}
		else
		{
			echo "You are wrong. Your answer is $q3  correct answer is 1920.";
		}	

//This is to check if question 4 is empty//
			if ($q4 == "") 
			{
			   die ("You forgot something, go back and check over your quiz.");
			}
			
//This is what happens if question 4 is not empty.//
//I think I need to put this in an isset but i am not sure because I dont want anything displayed if there is nothing in the field.//
			if ($q4 == False)
			{
				echo "You are Correct!";
				$score++;
			}
			else
			{
				echo "You are wrong. Your answer is $q4  correct answer is False.";
			}	


//This is to check if question 5 is empty//
				if ($q5 == "") 
				{
				   die ("You forgot something, go back and check over your quiz.");
				}
				
//This is what happens if question 5 is not empty.//
//I think I need to put this in an isset but i am not sure because I dont want anything displayed if there is nothing in the field.//
				if ($q5 == Tiger)
				{
					echo "You are Correct!";
					$score++;
				}
				else
				{
					echo "You are wrong. Your answer is $q5  correct answer is Tiger.";
				}	

//If the name is empty//
if ($name == "") 
{
   die ("You forgot something, go back and check over your quiz.");
}

//if the email is empty//
if ($email == "") 
{
   die ("You forgot something, go back and check over your quiz.");
}

//this mods the score by 5 to give a %//
$percent = $score % 5;

//this tells you your name and the score and percentage you recived on the quiz//
{echo "<p>Results: $name</p>";<br/>
echo "<p>Your score is $score out of 5. Your percentage is: $percent </p>";
//this takes you back to do the quiz again//
<p><a href="$zodquiz">Go Back To Quiz?</a>
}
?>
</body>
</html>
(This is ZodiacQuizResults.php)
Post Reply