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

contact.php on Shirts 4 Mike not working

I am following along with the "Shirts 4 Mike" PHP track and coding my php exactly like the instructor yet my page does not redirect to the "Thanks for the email!" portion of the php file. The instructor makes no mention of any special editing that needs to be done on apache or php. How can I get it to work. Thanks. Below is my contact.php code.

<?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;

// TODO: Send Email

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

} ?>

<?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 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="name">Email</label> 
                        </th>
                        <td>
                            <input type="text" name="email" id="email">
                        </td>
                    </tr>
                        <tr>
                        <th>
                            <label for="name">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'); ?>

6 Answers

ok so i got home and checked my code, now i see your problem, in the $_SERVER function you typed - $_SERVER["REQUEST_METHOD"] == $_POST but it should be like this $_SERVER["REQUEST_METHOD"] == "POST". and as befor the header function should stay the same just type - exit; after it.

From what i can see after the header("Location: contact.php?status=thanks"); there is no escape code and you are missing a " after the status in the header - header(location: contact.php?status="thanks") but you have status=thanks". After you fix that ype exit; after the header function and it should work, i hope =P.

The instructor has like this in the video and it works for him header("Location: contact.php?status=thanks");

I tried like you suggested which is this header(Location: contact.php?status="thanks");

But it causes an error in that line Parse error: syntax error, unexpected ':' in C:\wamp\www\shirts4mike\contact.php on line 15

oops my bad .... leave it like it was... forgot for a sec how the header function works... but just type exit; after the header function.

OK. Here is an even simpler code that still doesn't work. Page1.php and page2.php. Page1 code below. Page1 will not redirect to page for some reason. What is wrong? Thanks.

<?php if($_SERVER["REQUEST_METHOD"] == $_POST) { header("Location: page2.php");
exit; } ?>

<html>

<form action="page1.php" action="post">
    <input type="submit" value="go to page two">
</form>

</html>

Thanks Victor.