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

Blank page with form data shown after submitting

after filling in the data into the input fields i'm presented with a blank page and this:

Name: name Email: email@email.com Message: testmessage

What's wrong with my code?

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = $_POST["name"];
    $email = $_POST["email"];
    $message = $_POST["message"];
    echo "Name: " . $name . "</br>";
    echo "Email: " . $email . "</br>";
    echo "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="section page">
<div class="wrapper">
        <h1>Contact</h1>

<?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") { ?>
        <p>Thanks for the Email! Ill Be in touch shortly.</p>
<?php } else { ?>

        <p>We would love to hear from you! Complete the form to send us 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>
    </div>

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

Edited to format code. Please select edit to see what I did for future reference.

2 Answers

Jeff Lemay
Jeff Lemay
14,268 Points

I can't tell specifically because you lost some formatting when you pasted your code in... but is the exit command commented-out along with the header function? It should be. If not, you are exiting after echoing the name/email/message variables instead of continuing on to your page content (header/page/footer).

I agree with removing the exit.