Page 1 of 1

How to get results from quiz

Posted: Fri May 09, 2008 12:21 pm
by farwestvillager
I have this car quiz, but I don't know how to get the results of the many people who will most likely take the quiz (haha, it'll probably be just me taking the quiz). I will post the code here:

So...the quiz is fine except for the fact that I don't get the answers that people enter. How can I get those send to me or compiled in some place? A leaderboard would also be cool, but that can come later.

Thanks!

Code: Select all

 
 
<?php
$qid = "Car Quiz";
?>
<html>
<head>
<title><? echo " $qid"; ?></title>
</head>
<body>
<p><h3><? echo " $qid"; ?></h3></p>
<form action="results.php" method="post">
<p>Username: <input type="text" name="name"></p>
<p>1.) What car is this?<br>
<p><img src="http://www.alexbrew.com/enzo.jpg"></P>
<input type="radio" name="q1" value="Answer 1"> Ferrari Enzo<br>
<input type="radio" name="q1" value="Answer 2"> Ferrari F50</p>
<p>2.) What car is this?<br>
<p><img src="http://www.alexbrew.com/bmw_135i.jpg"></P>
<input type="radio" name="q2" value="Answer1"> BMW 540i<br>
<input type="radio" name="q2" value="Answer2"> BMW 135i</p>
<p>3.) What car is this?<br>
<p><img src="http://www.alexbrew.com/bmw_beater.jpg"></P>
<input type="radio" name="q3" value="Answer1"> Chevy Silverado<br>
<input type="radio" name="q3" value="Answer"> BMW Beater</p>
<p>4.) What car is this?<br>
<p><img src="http://www.alexbrew.com/ford_focus.jpg"></P>
<input type="radio" name="q4" value="Answer1"> Mercedes E350<br>
<input type="radio" name="q4" value="Answer2"> Ford Focus</p>
<p>5.) Question number five is?<br>
<p><img src="http://www.alexbrew.com/Hummer.jpg"></P>
<input type="radio" name="q5" value="Answer1"> Hummer H2<br>
<input type="radio" name="q5" value="Answer2"> Toyota Prius</p>
<p><input type="submit" name="submit" value="Submit Quiz"></p>
<input type="hidden" name="qp" value="quiz00.php">
</form>
</body>
</html>
 
 

Re: How to get results from quiz

Posted: Fri May 09, 2008 2:07 pm
by flying_circus
You need to make a results.php something like this:

Code: Select all

<?php
  # Get the results of the form
  $q1 = (isset($_POST["q1"])) ? $_POST["q1"] : '';
  $q2 = (isset($_POST["q2"])) ? $_POST["q2"] : '';
  $q3 = (isset($_POST["q3"])) ? $_POST["q3"] : '';
  $q4 = (isset($_POST["q4"])) ? $_POST["q4"] : '';
  $q5 = (isset($_POST["q5"])) ? $_POST["q5"] : '';
 
  # Score the results
  if(!empty($q1) && $q1 == "Answer 1") {
     print "question 1 is correct";
  } else {
     print "question 1 is incorrect";
  }
 
  // ... and so on ... //
?>

Re: How to get results from quiz

Posted: Sat May 10, 2008 12:31 pm
by RobertGonzalez
You are also going to want to database those results.

Re: How to get results from quiz

Posted: Mon May 12, 2008 9:28 pm
by farwestvillager
Thanks for the help.

I have the following code I found on a website, but something isn't working and I can't figure out what it is:

Code: Select all

<?php
$title = "Quiz Results";
echo "<title>$title</title>";
if (isset ($_POST['submit'])) {
nbsp; $name = $_POST['name'];
nbsp; $q1 = "1.)" . $_POST['q1'];
nbsp; $q2 = "2.)" . $_POST['q2'];
nbsp; $q3 = "3.)" . $_POST['q3'];
nbsp; $q4 = "4.)" . $_POST['q4'];
nbsp; $q5 = "5.)" . $_POST['q5'];
nbsp; $qp = "6.)" . $_POST['qp'];
}
if ($name == "") {
nbsp; die ("You forgot something, go back and check over your quiz.");
}
if ($q1 == "1.) ") {
nbsp; die ("You forgot something, go back and check over your quiz.");
}
if ($q2 == "2.) ") {
nbsp; die ("You forgot something, go back and check over your quiz.");
}
if ($q3 == "3.) ") {
nbsp; die ("You forgot something, go back and check over your quiz.");
}
if ($q4 == "4.) ") {
nbsp; die ("You forgot something, go back and check over your quiz.");
}
if ($q5 == "4.) ") {
nbsp; die ("You forgot something, go back and check over your quiz.");
}
{
echo<<<EOT
<p>Results: $name<br>
$q1<br>
$q2<br>
$q3<br>
$q4<br>
$q5</p>
<p><a href="$qp">Go Back To Quiz?</a>
 
EOT;
}
$Name = $_POST['name'];
$Subject = "Quiz Results";
$Email = "me@gmail.com";
$Message = "Here are the quiz results.";
$align = $_POST['align'];
$to = "$EmailTo";
$subject = "$Subject";
$body = "$Message\n\nQuiz By: $Name\n$q1\n$q2\n$q3\n$q4\n$q5";
mail($to,$subject,$body);
header("Location: mysite.com/sent.htm");
?>
My understanding is that this script will send me an email with a person's results after taking the quiz. Am I completely wrong? Something like that would be nice because I want to be able to see how people do on the quiz.

Re: How to get results from quiz

Posted: Mon May 12, 2008 9:53 pm
by RobertGonzalez
Wow, that is some craptastic code.

Are you developing with error_reporting set to E_ALL and display_errors on?

Re: How to get results from quiz

Posted: Mon May 12, 2008 10:19 pm
by farwestvillager
Ha. No. I'm developing on this web-based thing that isn't that great. Of course, I really have no clue what I'm doing, so I guess it fits my level of php/programming abilities.

But any ideas how I can get the results from my quiz to go somewhere practical like to my email inbox?

Re: How to get results from quiz

Posted: Mon May 12, 2008 11:14 pm
by RobertGonzalez
This is a very simple one field example of what you could do.

For more fields you could use extract() or a foreach to loop through the post array and check values. This has not been tested. Use for development only.

Code: Select all

<?php
$sent = false;
 
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  // A form was posted
  $user = $_POST['user'];
  if (!empty($user)) {
    // The form field was not empty so send it
    $to = 'you@yourdomain.com';
    $subject = 'A message has been sent by your web site';
    $message = "$user has sumitted his/her name through your web form";
    $headers = 'From: web@yourdomain.com' . "\r\n" .
      'Reply-To: web@yourdomain.com' . "\r\n" .
      'X-Mailer: PHP/' . phpversion();
    if (!mail($to, $subject; $message, $headers)) {
      die('Could not send the mail');
    }
    $sent = true;
  }
}
 
if ($sent) {
  echo '<p>The message was sent.</p>';
}
?>
<form method="post" action="<?php echo basename(__FILE__) ?>">
<p><input type="text" name="user" /></p>
<p><input type="submit" name="mylittlesubmitbutton" value="Send my name" /></p>
</form>

Re: How to get results from quiz

Posted: Wed May 21, 2008 9:59 am
by farwestvillager
Is the above code missing a terminating semicolon? I keep getting a "Parse error: syntax error, unexpected T_VARIABLE" message.

Re: How to get results from quiz

Posted: Wed May 21, 2008 11:09 am
by RobertGonzalez
Line 15 in my snippet:

Code: Select all

if (!mail($to, $subject; $message, $headers)) ?>
After the $subject var, replace the semicolon with a comma.