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

How to get quiz results emailed to me and the user?

  • Hi,
    I'm just a beginner and I created a quiz on my website using the tutorial at http://css-tricks.com/building-a-simple-quiz/
    I need to get the results when a User does the quiz emailed to me and them (they will provide their email address in a field below the quiz).
    Please provide clear step by step instructions as I am just starting out with this stuff.
    Thanks
  • The simplest method to write would be to use mail():

    http://php.net/manual/en/function.mail.php

    Please note that this isn't well suited to sending large numbers of emails, or HTML emails. However, if it's just a simple message to you and one other, I think that's your best option. Read the examples on the linked to page to understand how it works. If you understand how to use strings in PHP, then you should understand how it is working. If you don't understand how to use strings in PHP, then now would be a good opportunity to learn!
  • I'm really not getting this. Could someone please get me started on the code for the mail()? I did some but it didn't work. I need the results of the quiz to be emailed to the user and me, and I also need the quiz to still use grade.php to check the results and allow the user to see their score immediately. Here is the original code:
    <!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>
  • If you want anything personalised, you'll need to include your PHP code. Otherwise I'll just be posting here what is already in the page I linked to.

    Also, I don't see an email input in the code you posted above.
  • Sorry, I'm really new with this and I'm only in High School. I guess I'll need to go through this step by step.
    So I've created my PHP Quiz. It's working and the answers are getting checked with the grade.php file, and its displaying a correct score. What's the next step?

    Thanks for helping
  • This is the code for the grade.php page, I don't know if I'll need it. BTW I'm using Dreamweaver CS5.5.
    <!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>";
    ?>

    </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>
  • So, the PHP you posted here is different from the code you posted in the almost duplicate thread you posted:

    http://css-tricks.com/forums/discussion/18978/how-do-i-get-php-quiz-results-emailed-after-being-graded

    The other thread has code with the following:

    $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);


    I would suggest changing it to:

    $to = "admin@spongebob-fanclub.com";
    $subject = "Your Trivia Results";
    $message = "Here are your Trivia Results:\n ";
    $message .= $totalCorrect . " / 5 correct";
    $from = "me@example.com";
    $headers = "From: $from";

    // Send email
    mail($to,$subject,$message,$headers);


    You will need a real server for this to work (it's unlikely that you have the neccessary binaries available if you are running on your home machine) and your mail server may filter it out as spam, since the mail claims to be from example.com but in actuality isn't. Let me know if this works for you.
  • Thanks for the suggestion. I got it to send an email to me by using the code below. Now, I need the same message emailed to the user. I have already created an email address field on the quiz page.

    Thanks!

    <?php

    $mailTo = "admin@spongebob-fanclub.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 SpongeCash!\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 .= "Remember, you can earn SpongeCash for each Trivia once a week up to five times!\n\n";
    $msgBody .= "Thanks for Playing!\n\n";
    $msgBody .= "The Spongebob Fanclub";
    $xHeaders = "From:";

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

    ?>
  • Please Help!
  • Please do some research!

    What you want to do is literally already done five times in the example you posted above. Of course, you might also want to verify that the user has actually submitted a real email address, but it doesn't sound as though it is that you are struggling with. If you want someone to write your website for you, maybe you should ask them to do that? If you want help, then it would be nice if it at least looked like you had put a little effort in yourself.