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 Form header()

Hi,

I have problems with my contact form. After posting the information I get a blank page, as if the header function was not working properly.

Here is my code, can anyone help fixing it ?

Thanks!

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

// To do: 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'); ?>
Michael O'Malley
Michael O'Malley
4,293 Points

I copied your text verbatim and it works for me..

.> <.<

Thanks! I did the same with the code provided by Treehouse, replacing it little by little by mine. Until I discovered it then work out, without changing anything.

Do you think it's because I started coding on line 2 in the editor ?

Thanks!

Roxane

1 Answer

Nick Pettit
STAFF
Nick Pettit
Treehouse Teacher

Hi Roxane Castelein,

Just checking in on some unanswered forum posts. Were you able to figure this out?

Yes, thanks. The code was fine, I don't know why it was not working. I just created a new 'contact.php' copied pasted the code and it suddenly worked. Do you have any idea why this happened ?

Thanks,

Roxane

Nick Pettit
Nick Pettit
Treehouse Teacher

I'm not really sure. Could have been any number of things with the code, the browser, the environment... Sometimes refreshing the page or starting a new file is all it takes. :)