Hi,
I created a simple PHP Quiz using http://css-tricks.com/building-a-simple-quiz/
Then I edited the code of the php file so that the quiz results get emailed to myself using the mail() function.
After that I created a text field on the quiz page where the user can type in their email address.
Now I need the quiz results (the same message that gets emailed to me) to also be emailed to the user. Any suggestions?
I am a beginner, so please don't make it too complicated!
Any assistance would be greatly appreciated!

Here is the code so far:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<title>PHP Quiz</title>

<link rel="stylesheet" type="text/css" href="css/style.css" />
</head>

<body>

<div id="page-wrap">

	<h1>TRIVIA #1</h1>

    <?php

        $answer1 = $_POST['question-1-answers'];
        $answer2 = $_POST['question-2-answers'];
        $answer3 = $_POST['question-3-answers'];
        $answer4 = $_POST['question-4-answers'];
        $answer5 = $_POST['question-5-answers'];

        $totalCorrect = 0;

        if ($answer1 == "Question 1 - Correct") { $totalCorrect++; }
        if ($answer2 == "Question 2 - Correct") { $totalCorrect++; }
        if ($answer3 == "Question 3 - Correct") { $totalCorrect++; }
        if ($answer4 == "Question 4 - Correct") { $totalCorrect++; }
        if ($answer5 == "Question 5 - Correct") { $totalCorrect++; }

        echo "<div id='results'>$totalCorrect / 5 correct</div>";
        echo "<div id='results'>You earned $totalCorrect 0 Coins</div>";
    ?>
<?php

  $mailTo = "admin@mywebsite.com";
  $msgSubject = "Your Trivia Results";
  $msgBody = "Hi!\n\n";
$msgBody .= "On Trivia #1 you got ";
$msgBody .= $totalCorrect . " out of 5 correct!\n ";
$msgBody .= "You earned ";
$msgBody .= $totalCorrect . "0 Coins!\n\n";
$msgBody .= "Here are your Trivia Results:\n";
$msgBody .= $answer1 . "\n";
$msgBody .= $answer2 . "\n";
$msgBody .= $answer3 . "\n";
$msgBody .= $answer4 . "\n";
$msgBody .= $answer5 . "\n\n";
$msgBody .= "Thanks for Playing!\n\n";
  $xHeaders = "From:";

  mail ($mailTo, $msgSubject, $msgBody, $xHeaders);

?>
	</div>

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-68528-29");
pageTracker._initData();
pageTracker._trackPageview();
</script>

</body>

</html>

    Store their email data into a variable:

    $email = $_POST['email_field_name'];
    

    Then validate it using:

    if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
        //if the if statement executes, this means it is a valid email address....
    }
    else{
         echo "The email you entered was invalid. Please try again. You will be redirected shortly...";
         header("refresh:5, url=your_form_page.php");
    }
    

    Then just do exactly the same as you have done for the email to be sent to you, but just write code to assist their email address as such:

    $mailTo = $email;
    

    Hope this helps somewhat.

    Kind regards,

    Labtec

      6 years later

      Thanks for this! I have a follow-up question:

      How can I get the email to send the written-out answer of the multiple choice answer they chose?

      For instance, the label for the radio button they selected (ex: the "<label for="question-4-answers-B">") instead of just the "value" (A, B, C, or D)

      Thanks!

        Write a Reply...