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 trialBen Goldman
14,626 PointsPHP: $_Get and $_Post
I'm reading back through my code from the contact form section of Build A Simple PHP App, and I'm trying to understand exactly how $_Get and $_Post work.
Am I correct in assuming that, unless you specify otherwise, all data sent to the server will be read as "post", and all date retrieved from the server will be read as "get"?
5 Answers
Kevin Korte
28,149 PointsOkay, here is the logic behind this.
Your form's method is post, and it's set to submit to itself, which is contact.php.
So when the form is submitted, the info gets set to the top. It says if the method is "POST" (it is) than we save the name, email, and message inputs into a their own variables $name, $email, and $message.
Than, we build our email body using the $email_body variable and the variables we just stored the form contents in.
Finally, we set the header location to basically append ?status=thanks
to the end of our page URL and than exit this code.
Now the page has to reload as well, and we are going to have one of two URLs. It's either going to be /contact.php
or it's going to be /contact.php?status=thanks
.
Now our $_GET conditional says if a "status" is set, AND that "status" is set to "thanks", than display the "Thanks for your email" text. If "status" is not set in the URL, than display the contact form.
We use POST to submit the form data and set "status=thanks" in the URL, we have to use GET to check and see if those values are set in the URL, and than we will know if the user submitted the form or not.
The only issue with the problem is somebody could specifically go to the `contact.php?status=thanks
URL and see the "Thanks for your email" text instead of the form, without actually submitting the form.
Richard Duncan
5,568 PointsThe GET HTTP method requests are used to get data from a specific resource, the parameters and values can be seen in the URL for example...
..index.php?param1=value1¶m2=value2
POST requests submit data to be processed.
If you fail to specify a method in your form then it will default to GET. Best to define the method and then in your PHP code when accessing that data you know which to use rather than relying on defaults. There are several differences between the methods that may influence your decision on which you want to use, in general when passing data with my own code I use POST.
Ben Goldman
14,626 PointsI guess I should have been more specific. What I'm actually having trouble with is following the flow of this page of 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;
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') ?>
What mostly confused me is the conditional. It says to only run certain parts of the code based upon whether or not they are POST or GET. However, it seems to me that that status hasn't already been defined anywhere. So how does it work?
Richard Duncan
5,568 PointsMike is using the same page for guests to contact him. The conditional is being used to determine what data to show to the user.
In plain English, if the method is equal to post then display the message in the email body redirect the user and exit the script.
If the method is get AND the status equals thanks then display the message thanks I will be in touch shortly.
For every other condition display the input form.
The user flow here would be fill in the form, submit the form, the form is processed with the post condition check, the user is redirected back to the contact page with the status of thanks, the get condition is invoked and the thank you message is displayed.
Ben Goldman
14,626 PointsThanks, everyone. Your answers have been extremely helpful.
I overlooked the form method being POST. I think that was the missing link I was looking for.
Thanks, again!