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

alan heath
alan heath
5,188 Points

Contact.php page no longer shows after adding "if(isset($get["status"]" part.

<?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");
    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 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'); ?>

6 Answers

alan heath
alan heath
5,188 Points

Okay...I decided to go back and redo the whole lesson and for some reason it all works now. Unfortunately i still have know idea what I did wrong the first time. It had to be some type of syntax error.

Yay for it working! You solved your problem so I've awarded you with best answer.

If you were super interested in finding out what was wrong, you could always 'diff' the two files. In other words, some text editors / command lines tools will automatically compare two files, and tell you what's different.

If you're interested to find out, let me know what text editor you're using :-)

Justin Black
Justin Black
24,793 Points

That would mean you have an error in your code somewhere. Without testing the entire thing, just the if else statement works as it should.

If it's returning a blank white screen, look into editing your php.ini file, to turn error_reporting on.

You can also do this within your script itself by putting: error_reporting(-1); at the beginning of the file just after the opening php tag.

To summarise the code Justin Black mentioned:

<?php

error_reporting(-1);
ini_set('display_errors', 'On');

It looks like you have a syntax error on your very first if statement:

<?php

if ($_SERVER["REQUEST_METHOD"]) == "POST") {

should be

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST") {

Notice one bracket less after $_SERVER['REQUEST_METHOD']

alan heath
alan heath
5,188 Points

I added the error code statements and still it only displays a white page.

Michael Dowell
Michael Dowell
2,390 Points

I have the same issue. This line of code works:

<?php if ($_GET['status'] == "thanks"){ ?>

As expected, here is the error message displayed:

Notice: Undefined index: status in /Applications/MAMP/htdocs/contact.php on line 31

I also tested by replacing with isset code. This new line of code works with no errors reported:

<?php if (isset($_SESSION['status'])){ ?>

However, when I try to use AND to confirm both conditions are true I get a 500 error:

<?php if (isset($_SESSION['status'])) AND ($_GET['status'] == "thanks"){ ?>

Here is my complete code:

<?php

error_reporting(-1);
ini_set('display_errors', 'On');

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = $_POST["name"];
    $email = $_POST["email"];
    $message = $_POST["message"];
    $email_body = "";
    $email_body .= "Name: " . $name . "<br />" . 
    "Email: " . $email . "<br />" . 
    "Message: " . $message . "<br />";

    // TODO: Send Email.

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

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

        <div class="section page">

            <div class="wrapper">

                <h1>Contact</h1>

                <?php if ($_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="">

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

Any suggestions? I'm thinking there is some weird syntax error that might be fixed by simply retyping everything as OP mentioned.

alan heath
alan heath
5,188 Points

One of the things that screws me up(pardon my language) is extra spaces where there shouldn't be. With PHP syntax has to be perfect. So that may of been my problem in this exercise. So that is why I redid the whole lesson. Go thru and compare the instructors typing with yours and see if you can spot the error that way.