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
Nicholas Renfroe
7,739 PointsWorking with Get Variables...need help!
I'm having trouble with the project in PHP about "Working with Get Variables" video. It's about creating a simple contact form using PHP of course.
What I'm not getting is when I run my localhost contact page for this project, everything works great, until I finish the contact form. Once filled out, and submitted, it's supposed to redirect you to a thank you status. For some reason, when I submit mine, it sends me right back to a blank contact form again, instead of the thank you status and note.
Here is my 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;
// 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! I’ll be in touch shortly.</p>
<?php } else { ?>
<p>I’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'); ?>
Could someone review this code for me, and lend any advice. Thanks in advance!
4 Answers
Leonardo Hernandez
13,798 Points$_SERVER['REQUEST_METHOD']
Sean T. Unwin
28,690 PointsI compared your code to mine, at the stage you are at, and it is practically identical so I pasted your above snippet into my t-shirt project and named the file contact2.php. I loaded the page, filled the form and I got the message as intended, but not before changing,
$SERVER["REQUEST METHOD"] == "POST"
to
$_SERVER["REQUEST_METHOD"] == "POST"
edit: I see Leonardo Hernandez found it also (have an upvote).
I did notice one minor point, which really is just semantic for HTML5 --
<input type="text" name="email" id="email">
may be changed to:
<!-- change type from 'text' to 'email' -->
<input type="email" name="email" id="email">
The above won't have any impact on the issue you're having, it was simply something I noticed.
Nicholas Renfroe
7,739 PointsThanks Sean, for the info, I'm still kinda fresh at all this, so anything helps!
Leonardo Hernandez
13,798 PointsYour conditional for checking if the get variable has been set and equals something is correct, but the problem is you have not coded your form to post a get variable, or what its value is via its action.
<form method="post" action="contact.php">
If this is all on the contact.php which i understand it is, remove your current action, and change it to set the get variable.
Sean T. Unwin
28,690 PointsThis isn't needed as the status variable is set on the redirect in the first if statement with header("Location: contact.php?status=thanks");, which, in turn sets some variables to be used send mail later in the lesson within the same php doc.
Leonardo Hernandez
13,798 PointsTrue Sean. Is there an underscore missing between request method?
Nicholas Renfroe
7,739 PointsThank you all for your time and help! Yes, you all are correct, I left out two underscores. in $SERVER and "REQUEST METHOD". They should be $_SERVER and "REQUEST_METHOD". I changed that, ran my pages, and boom, all good to go! Thanks again Treehouse community for being a wealth of knowledge!