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. I can't find the mistake in the code can you?

This is the final code for the contact.php page from the Adding A Contact Form Section of Build A Simple PHP Application. I've looked and looked and commented out etc. I cannot find the mistake. I've had an issue with the header("Location:contact.php") section since two videos ago. When I debugged that though everything worked except for when I added the header("Location:...) Contact.php hasn't rendered the thank you message since 2 videos ago. Now nothing renders when I use this code. Just a blank page. Any ideas?

<?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('inc/header.php'); ?>

<div class="section page">
       <div class="wrapper">
    <h1>Contact</h1>

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

        <p>Thanks 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 type = "textarea" name="message" id = "message"></textarea>
                                </td>
                                </tr>
                    </table>
                    <input type="submit" value="send">
                    </form>
                    <?php } ?>
</div>
</div>

4 Answers

Thiago van Dieten
Thiago van Dieten
17,059 Points

Hello,

Your code goes all well until it reaches this point:

<?php} else { ?>

There's no whitespace between <?php and }, and <?php} is not something it understands. Fixing this allowed me to see the form on the contact page. :)

I hope this helped you with your project!

-Thiago

Thank you Thiago. So I've closed up the white spaces. Still no luck. Is it my local machine? I'm running MAMP on Macbookpro, OS X 10.8.3 Here's the new code:

<?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('inc/header.php');?>

<div class="section page">
<div class="wrapper">
    <h1>Contact</h1>

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

        <p>Thanks 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 type = "textarea" name="message" id = "message"> </textarea>
                                </td>
                                </tr>
                    </table>
                    <input type="submit" value="send">
                    </form>
                    <?php}?>
</div>
</div>
Thiago van Dieten
Thiago van Dieten
17,059 Points

Haha, I think I've explained this wrong, so my apologies.

What I meant is that there should be a white space between <?php and the square brackets (same goes for square brackets and ?>)

Hope this helps,

Thiago

Thank you Thiago. That fixed the contact.php appearing. However, when I fill in the form and click submit? I do not get the "Thank you I'll be in touch shortly" redirect that is coded in the header("Location:contact.php?status=thanks").

This didn't work throughout the whole exercise for me not just on this page. Any ideas? (should this be a separate post?)

Thiago van Dieten
Thiago van Dieten
17,059 Points

Could you c&p contact.php again? I've got "Thank you I'll be in touch shortly" to work with the 1st file you've send after applying the correct white spacing.

<?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('inc/header.php'); ?>

<div class="section page">
<div class="wrapper">
    <h1>Contact</h1>

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

        <p>Thanks 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 type = "textarea" name="message" id = "message"> </textarea>
                                </td>
                                </tr>
                    </table>
                    <input type="submit" value="send">
                    </form>
                    <?php } ?>
</div>
    </div>
Thiago van Dieten
Thiago van Dieten
17,059 Points

Having that copy & paste'd into a new file, everything still is working just fine for me. Does it work when you enter contact.php?status=thanks in your browser bar?

Yes. The thank you renders when I enter contact.php?status=thanks However, nothing at all renders when I click the send button whether I fill the form out or not. Just a blank page. And the title bar has http://localhost/contact.php in it.