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

header redirect isn't working

The header redirect isn't working.. As far as I see I don't ouput anyhting before the header(); and when I do a check and create a test page named test.php and create the following the redirect works just fine.

<?php$url = header("Location: contact.php?p=thanks"); var_dump($url);?>

I do request some help here!

Thanks in advance, Sjors

<?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 . "E-mail: " . $email . "\n";
    $email_body = $email_body . "Message: " . $message;

    // TODO: send Email

    header("Location: contact.php?p=thanks");
    exit;   
}
?>
<?php
$pageTitle = "Contact Mike";
$section = "contact";
include("project0/inc/header.inc.php"); ?>

    <div class="section page">

        <div class="wrapper">

            <h1>Contact</h1>

            <?php if (isset($_GET["p"]) and $_GET["p"] == "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 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">E-mail</label>
                            </th>
                            <td>
                                <input type="text" name="email" id="email">
                            </td>   
                        </tr>
                        <tr>
                            <th>
                                <label for="email">Message</label>
                            </th>   
                            <td>
                                <textarea name="message" id="message"></textarea>
                            </td>
                        </tr>
                    </table>
                    <input type="submit" value="Send">

                </form>

            <?php } ?>

        </div>

    </div>

<?php include("project0/inc/footer.inc.php"); ?>

3 Answers

Okay got it working myself! After some hours debugging again the problem proved itself to be something small yet again. Keeping in mind that PHP == Casesensitive I looked at my code and thought hmmm... "post" is written in lowercase, let's change it to uppercase. And voilla it worked magically! For educational purposes I hope other people will find this helpful.

You have an equals (=) sign right after the question mark (?). That might be the problem to you issue. Change the redirect statement to the following:

header("Location: contact.php?p=thanks"); That should work for you.

True that was a valid and helpfull comment! While trying to debug I accidently added it and uploaded it istead of the original script. The main script however doesn't contain that equal sign and still doesn't work.

corrected the above mistake