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!
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.
<?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.
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?
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.
<?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>