Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Build a Simple PHP Application Adding a Contact Form Working with Get Variables

Tom McHenry
Tom McHenry
5,264 Points

Working with Get Variables

I can't seem to get the thank you page to display. The form submits but then returns to itself. Any help would be appreciated.

if ($_SERVER["REQUEST_METHOD"] == "POST"){
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];
$email_body ="";
$email_body = $email_body . "Name: ".$name . "\n";
$email_body = $email_body . "Email: ".$email . "\n";
$email_body = $email_body . "Message:".$message;

// TODO: Send Email

header("Location: contact.php?$status=thanks");
exit;
}
?>

<?php
$pageTitle = "Contact Mike";
$section = "contact";
include('inc/header.php');
?>

<div class="section page">
    <div class="wrapper">
    <h1>Contact</h1>
    <?php if (isset($_GET["status"]) AND  $_GET["status"] == "thanks") { ?>
    <p>Thanks for the email! I&rsquo;ll be in touch shortly!</p>
    <?php } else { ?>
        <p>I&rsquo;d love to hear from you! Complete this form to send me an e-mail</p>
        <form method="post" action="contact.php">
        <table>
        <tr><th>
        <label for="name">Name</label></th><td><input type="text" name="name" id="name">
        </td></tr>
            <tr><th>
            <label for="email">E-mail</label></th><td><input type="text" name="email" id="email">
        </td></tr>
            <tr><th>
            <label for="message">Message</label></th><td><textarea name="message" id="message"></textarea>
        </td></tr>
        </table>
        <input type="submit" value="Send">
        </form>
    <?php } ?>
    </div>
</div>
<?php include('inc/footer.php'); ?>

4 Answers

Why is there a $ in

<?php

header( "Location: contact.php?$status=thanks" );

Correct me if I'm wrong here. When you use double quotes, PHP will parse any strings you pass in, looking for variables. So, when you pass your string into the header function, PHP looks for any variables included. Since it finds that $ in there, it thinks that $status is a PHP variable. I'm not 100, but it doesn't look like you've declared $status anywhere, and additionally, I'm not sure that you want to.

Try replacing the double quotes with single quotes and/or removing the $ from in front of status.

I see no opening PHP tag...could that be the problem? Half the time, the answer is a silly thing like a missing semicolon or quote, so that's why I ask.

Tom McHenry
Tom McHenry
5,264 Points

Sorry, it's actually there in the real page, it looks like the markdown code isn't displaying the opening tag?

Tom McHenry
Tom McHenry
5,264 Points

That did it!

Thanks, Ryan!