Problem with simple quiz

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
sheepz
Forum Commoner
Posts: 58
Joined: Wed Jul 06, 2005 11:35 pm

Problem with simple quiz

Post by sheepz »

Sorry folks, just doing a beginners PHP book to learn, but cannot figure out what I'm doing wrong. I'm suppose to make a quiz with 3 questions one with INPUT TYPE="text", INPUT TYPE="radio", OPTION VALUE (drop down menu). For each question I need to assign 10 points if correct. Here's what I have so far. Here's the HTML page for fname, lname, and questions:

Code: Select all

<HTML>
<HEAD>
<TITLE>Quiz Project</TITLE>
</HEAD>
<BODY>


<!-- This will show what method to use and what file to use -->


<FORM METHOD=&quote;post&quote; ACTION=&quote;Calc.php&quote;>


<!--  First and last name entry boxes -->

Enter your first name:<br>
<INPUT TYPE=&quote;text&quote; NAME=&quote;fname&quote; SIZE=20></p>

Enter your last name:<br>
<INPUT TYPE=&quote;text&quote; NAME=&quote;lname&quote; SIZE=20><br></p>


<!-- Question 1 -->

Question 1:<br>
What is 3 + 5?:<br>
<INPUT TYPE=&quote;text&quote; NAME=&quote;question1&quote; SIZE=20><br></p>


<!-- Question 2 using input type radio dials -->

Question 2<br>
What is 3 * 5?<br>
<INPUT TYPE=&quote;radio&quote; name=&quote;question2&quote; value=&quote;10&quote;> 10<br>
<INPUT TYPE=&quote;radio&quote; name=&quote;question2&quote; value=&quote;15&quote;> 15<br>
<INPUT TYPE=&quote;radio&quote; name=&quote;question2&quote; value=&quote;20&quote;> 20<br></p>


<!-- Question 3 using input type drop down menu &quote;option&quote; -->

Question 3<br>
What is 3 - 5?<br>
<P><SELECT NAME=&quote;question3&quote;>
<OPTION VALUE=&quote;a&quote;>-2</OPTION>
<OPTION VALUE=&quote;b&quote;>-1</OPTION>
<OPTION VALUE=&quote;c&quote;>0</OPTION>
</SELECT></P>


<!-- The input button for final calculation -->

<INPUT TYPE=&quote;submit&quote; NAME=&quote;submit&quote; Value=&quote;Grade me!&quote;>

</FORM></BODY></HTML>

Here is my PHP calc.php page:

Code: Select all

<?
// Checks for missing values in these VARs
if (($_POST[fname]=="") || ($_POST[lname]=="") || ($_POST[question1]=="") || ($_POST[question2]=="") || ($_POST[question3]==""))

// This tells the code where to go if any of the top fields are missing values
{
  header("Location: assignment2.html");
  exit;
}

// Question1 results
if ($_POST[question1]=="8")
{
  $result1=10;
} else {
  $result1=0;
}

// Question2 results
if ($_POST[question2]=="15")
{
  $result2=10;
} else {
  $result2=0;
}

// Question3 results
if ($_POST[question3]=="a")
{
  $result3=10;
} else {
  $result3=0;
}

?>

<html>
  <!-- Output page -->
<head>
  <title>Quiz Results</title>
</head>

<body>

<?

$final=$result1 + result2 + result3;

echo "$_POST[fname] $_POST[lname] received $final</p>";


?>

</body>
</html>

I can only seem to get the $result1 to work, but $result2 and $result3 will not add up at all. Not sure if i'm doing it correctly, thanks =\

JCART | Please use

Code: Select all

tags when posting php code. Review [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

First, add quotes inside the brackets on $_POST's. ie

Code: Select all

$_POST["lname"]
everywhere....

Next add a print_r($_POST) to the top of your php script to see if its getting the values it should be.

[Ooops, or just add '$'s before result2 and result3 in your summing step :) ]
sheepz
Forum Commoner
Posts: 58
Joined: Wed Jul 06, 2005 11:35 pm

Post by sheepz »

OMG... i'm cut... i was staring at that screen for like 2 hours trying to figure it out... i overlooked and didn't see i was actually missing the $ for the $result2 and $result3... it works great! thx for the help =)
Post Reply