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

Ross Campbell
Ross Campbell
5,410 Points

Contact page won't load now

Hi solo back here with yet another question! Everyone has been so amazing on here so far so thanks!

I've hit another snag, i've done every part of stage 3 and everything has seemed fine until i came to combining the three contact files. CONTACT, CONTACT THANKS & CONTACT PROCESS together.

Now the contact page won't even load.

I've been through the video again a number of times. Been through my code comparing it to that in the video. been through other questions on the forum, nothing seems to work so far.

Here is my code hopefully someone can tell me what i'm missing.

<?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;
    header("Location: contact.php?status=thanks");
    exit;
} ?><?php 

$pageTitle = "Contact Mike";
$section = "contact";

include("Includes/header.php"); ?>

    <div class="section page">

        <div class="wrapper">

            <h1>Contact</h1>

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

                <p>Thank you for filling out the form</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>
                    }

        </div>

    </div>


<?php include("Includes/footer.php"); ?>
Ross Campbell
Ross Campbell
5,410 Points

Thank YOU soo much for all you help.

Not wrapping the closing brace for the form did the trick. Thanks so much, i'm trying to hone my bug finding skills but that one eluded me.

Thanks so much and good luck with all your work.

You're welcome and thanks!

5 Answers

I missed it the first time but the closing brace for your else block isn't wrapped in php tags.

                </form>
            <?php } ?>
        </div>

Hi Ross,

Is there any chance you changed your include folder in the process? Do you have your footer.php and header.php in a folder named "Includes" with a capital 'I'

I would recommend the consistent use of lowercase but as long as they match then that shouldn't be causing a problem.

Alexander Rasmussen
Alexander Rasmussen
12,901 Points

I have the same problem, but i remembered the <?PHP } ?> ???

Well, you could post your code here if you want.

<?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;
    echo $email_body;

    // 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! Ill keep in touch.</p>
        <?php }else{ ?>
        <p>I would love to hear from you</p>
        <form method="post" action="contact.php">
            <table>
                <tr>
                    <th scope="row"><label for="name">Name</label></th>
                    <td><input type="text" name="name" id="name"></td>
                </tr>
                <tr>
                    <th scope="row"><label for="email">Email</label></th>
                    <td><input type="text" name="email" id="email"></td>
                </tr>
                <tr>
                    <th scope="row"><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') ?>
Rob Adams
Rob Adams
3,079 Points

Hey Thanvir, your "exit;" command should be inside your $_SERVER if block, like this:

You Have:

// TODO: Send email

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

} exit; ?>

It should be:

// TODO: Send email

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

}

?>