treehouse : what would you like to learn today?
Web Design Web Development iOS Development

How do I get PHP Quiz Results emailed after being graded?

  • Hi,
    I created a PHP Quiz using the tutorial at http://css-tricks.com/building-a-simple-quiz/
    I need to get the quiz results emailed to me and the user, and graded. It already grades the quiz and displays a score.
    I'm really a beginner so I don't know how to get the results emailed. Someone suggested using the mail() function, with a link to this page, http://php.net/manual/en/function.mail.php, but I don't know how to use it at all. Could someone please give me detailed instructions - not just a link please.
    Help would be greatly appreciated!
    Here is the code for my quiz:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
    <html xmlns="http://www.w3.org/1999/xhtml"&gt;

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

    <title>Welcome to the Spongebob Fanclub! | Trivia #1</title>

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

    <body>

    <div id="page-wrap">

    <h1>TRIVIA #1</h1>

    <form action="grade.php" method="post" id="quiz">

    <ol>

    <li>

    <h3>In the KRUSTY TOWERS episode, what does Patrick say after Squidward says 'How can you have bags when you just found out this is a hotel?'</h3>

    <div>
    <input type="radio" name="question-1-answers" id="question-1-answers-A" value="A" />
    <label for="question-1-answers-A">A) This is a hotel?</label>
    </div>

    <div>
    <input type="radio" name="question-1-answers" id="question-1-answers-B" value="B" />
    <label for="question-1-answers-B">B) I just got them</label>
    .</div>

    <div>
    <input type="radio" name="question-1-answers" id="question-1-answers-C" value="C" />
    <label for="question-1-answers-C">C) I don't know.</label>
    </div>

    <div></div>

    </li>

    <li>

    <h3>In ROCK BOTTOM, what was the name of the Candy Bar Spongebob wanted out of the vending machine?</h3>

    <div>
    <input type="radio" name="question-2-answers" id="question-2-answers-A" value="A" />
    <label for="question-2-answers-A">A) Kelp Shake</label></div>

    <div>
    <input type="radio" name="question-2-answers" id="question-2-answers-B" value="B" />
    <label for="question-2-answers-B">B) Glove Candy</label></div>

    <div>
    <input type="radio" name="question-2-answers" id="question-2-answers-C" value="C" />
    <label for="question-2-answers-C">C) Kelp Bar</label>
    </div>

    <div></div>

    </li>

    <li>

    <h3>In ROCK BOTTOM, what is Spongebob's number in line at the Bus Station?</h3>

    <div>
    <input type="radio" name="question-3-answers" id="question-3-answers-A" value="A" />
    <label for="question-3-answers-A">A) </label>386
    </div>

    <div>
    <input type="radio" name="question-3-answers" id="question-3-answers-B" value="B" />
    <label for="question-3-answers-B">B) 329</label>
    </div>

    <div>
    <input type="radio" name="question-3-answers" id="question-3-answers-C" value="C" />
    <label for="question-3-answers-C">C) 369</label>
    </div>

    <div></div>

    </li>

    <li>

    <h3>In KARATE ISLAND, what does Udan want Spongebob to buy?</h3>

    <div>
    <input type="radio" name="question-4-answers" id="question-4-answers-A" value="A" />
    <label for="question-4-answers-A">A) A</label>
    Condo</div>

    <div>
    <input type="radio" name="question-4-answers" id="question-4-answers-B" value="B" />
    <label for="question-4-answers-B">B) A Yacht</label>
    </div>

    <div>
    <input type="radio" name="question-4-answers" id="question-4-answers-C" value="C" />
    <label for="question-4-answers-C">C) An Apartment</label>
    </div>

    <div></div>

    </li>

    <li>

    <h3>In MRS. PUFF, YOU'RE FIRED, when Spongebob takes apart the boat, what does he build when he puts it back together?</h3>

    <div>
    <input type="radio" name="question-5-answers" id="question-5-answers-A" value="A" />
    <label for="question-5-answers-A">A) A Car</label>
    </div>

    <div>
    <input type="radio" name="question-5-answers" id="question-5-answers-B" value="B" />
    <label for="question-5-answers-B">B) A Boat</label>
    </div>

    <div>
    <input type="radio" name="question-5-answers" id="question-5-answers-C" value="C" />
    <label for="question-5-answers-C">C) A Rocket</label>
    </div>

    <div></div>

    </li>

    </ol>

    <input type="submit" value="Submit Answers" />

    </form>

    </div>

    <script type="text/javascript">
    var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl.&quot; : "http://www.&quot;);
    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>
  • And here is the code for the grade.php file that is used to grade the quiz (go to 'http://css-tricks.com/building-a-simple-quiz/" for more info):
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
    <html xmlns="http://www.w3.org/1999/xhtml"&gt;

    <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 == "A") { $totalCorrect++; }
    if ($answer2 == "C") { $totalCorrect++; }
    if ($answer3 == "B") { $totalCorrect++; }
    if ($answer4 == "A") { $totalCorrect++; }
    if ($answer5 == "C") { $totalCorrect++; }

    echo "<div id='results'>$totalCorrect / 5 correct</div>";
    echo "<div id='results'>You earned $totalCorrect 0 SpongeCash</div>";
    ?>
    <?php
    // Set up parameters
    $to = "admin@spongebob-fanclub.com,'Email Address'";
    $subject = "Your Trivia Results";
    $message = "Here are your Trivia Results:";
    $from = "me@example.com";
    $headers = "From: $from";

    // Send email
    mail($to,$subject,$message,$headers);
    ?>
    </div>

    <script type="text/javascript">
    var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl.&quot; : "http://www.&quot;);
    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>