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

Kelly Ferrell
Kelly Ferrell
2,561 Points

On the GET variable video it is giving the error unexpected end. I'm not sure what I'm missing.

Got an unexpected ending error. I am not sure what I did wrong. Had anybody else had this problem.

9 Answers

Hello Kelly,

Here is the working 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;
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"]) && $_GET["status"]=="thanks"){?>
               <p>I&rsquo;d love to hear from you! Complete the form to send me an email</p>
        <?php } else { ?>

         <p> Thanks for the email! I&rsquo;ll be in touch shortly.</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');?>

I managed to get mine working like this, it's something to do with the <?php } ?> part after the closing form tag, it needed a space each side of the curly bracket for it to work. At least thats what I changed in order to get it working. :)

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Kelly;

Can you post your code using the Markdown Cheatsheet? It will help to get this issue resolved in a timely manner.

Thanks, Ken

Kelly Ferrell
Kelly Ferrell
2,561 Points

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

("Location: contact.php?status=thanks"); echo "test"; 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"){?> <?php } else {?> <p>I’d love to hear from you! Complete the form to send me an email</p> <p> Thanks for the email! I’ll be in touch shortly.</p> <form method="post" action="contact.php"> <?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('inc/footer.php');?>"""
Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Kelly;

For the Markdown posting you need to use three (3) backticks, not quotes. The backtick is located to the left of the "1" key, above the tab key on most keyboards.

When I put your code into my text editor I don't find an opening <form> tag. So the closing </form> tag could be an issue. Have you tried downloading the project files and then comparing your code with that?

Kelly Ferrell
Kelly Ferrell
2,561 Points
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"]) && ($_GET["status"]=="thanks"){?>
        <?php } else {?>
                <p>I&rsquo;d love to hear from you! Complete the form to send me an email</p>
         <p> Thanks for the email! I&rsquo;ll be in touch shortly.</p>
        <form method="post" action="contact.php"> <?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('inc/footer.php');?>```
Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Kelly;

You need a <?php } ?> between the </form> and the </div> tags. In your last posting of your code you are missing the opening <?php tag at the beginning of the file, but you had it in your previous post so it is probably just a cut and paste mishap.

Happy coding,

Ken

Kelly Ferrell
Kelly Ferrell
2,561 Points
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}?>
<?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"]) && $_GET["status"]=="thanks"){?>
        <?php } else {?>
                <p>I&rsquo;d love to hear from you! Complete the form to send me an email</p>
         <p> Thanks for the email! I&rsquo;ll be in touch shortly.</p>
        <form method="post" action="contact.php"> <?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');?>```
Kelly Ferrell
Kelly Ferrell
2,561 Points

I put <?php}?> after form. However I still getting an unexpected ending error. So I reposted the code....Help what am I doing wrong?

Kelly Ferrell
Kelly Ferrell
2,561 Points

Thanks you are the best.