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

Two issues on PHP contact page: "Notice: Undefined index: REQUEST_METHOD in line 3" ... Also, not able to redirect.

I'm getting the error: Notice: Undefined index: REQUEST_METHOD in c:\xampp\htdocs\contact.php on line 3

Also, when i submit the form, i'm not being redirected to the thank you page...

My code is:

<?php

if ($_SERVER["REQUST_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 . "\n";

// 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"]) && $_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 id="name" type="text" name="name">
                    </td>
                </tr>
                <tr>
                    <th>
                        <label for="email">Email</label>
                    </th>
                    <td>
                        <input id="email" type="text" name="email">
                    </td>
                </tr>
                <tr>
                    <th>
                        <label for="message">Message</label>
                    </th>
                    <td>
                        <textarea id="message" type="text" name="message"></textarea>
                    </td>
                </tr>
            </table>

            <input type="submit" value="send"> 

        </form>

    <?php } ?>

</div>

</div>

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

3 Answers

Jose Soto
Jose Soto
23,407 Points

Make your PHP conditional check the server method against an uppercase 'POST':

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") { 
Damien Watson
Damien Watson
27,419 Points

Hey Eric,

This may or may not help, but the 'e' is missing in "REQUST_METHOD":

<?php
if ($_SERVER["REQUEST_METHOD"] == "post") {
}
?>

As far as the redirect goes, I can't see anything wrong, but I believe there can't be anything written to the page, so make sure this code is at the very top (above all html).

Damien - Aha - Issue 1 resolved.

However, still not getting why my redirect isn't working.... It appears exactly as it does in the video... Figure it must be something in header() or my if () conditional, but just not seeing it.

Damien Watson
Damien Watson
27,419 Points

Eric, as Jose is saying, try the "POST" in capitals, it may be that the 'REQUEST_METHOD' is not evaluating because it is case sensitive. The header seems to be fine, so thats all I can think of.