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

[SOLVED] ?status=thanks won't execute

After submitting the form, it does not redirect to ?status=thanks.

<?php

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;

        // TO DO: 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; love to hear from you! complete the form to send me an email.</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">Email</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"); ?>

I just tried the code and it works here. What does happens when you post the form? What URL is retrieved? Does it just display the form again?

A blank page shows after I click submit. The url stays the same, http://localhost:8888/shirts4mike/contact.php but I know it should show http://localhost:8888/shirts4mike/contact.php?status=thanks.

If opening contact.php displays the form, but pressing submit displays a blank page, then maybe the IF statement is being triggered correctly but there is a problem with that block of code.

You could try something like this:

Remove (or comment out) everything inside the IF statement and replace it with a test message, so it looks like this:

if ($_SERVER["REQUEST_METHOD"] == "POST") { 
    echo "POST request";
    exit;
}

Then open contact.php and submit the form. If you see the message, then you know everything is working apart from the code you commented out. Then you can add back each part of the previous code and identify the failure. Does this make sense?

Take a look at the code here: http://phpassist.com/937d8#2

It works correctly so you might be able to see the difference by comparison.

Thanks for the help, it works now

Marked as solved