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
Samuel Chun
8,890 PointsChecking the request method
What is the purpose of checking if the $_SERVER[" REQUEST_METHOD"] is POST?
What else could it be?
<?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;
}
?>
1 Answer
Andreas cormack
Python Web Development Techdegree Graduate 33,011 PointsHi Samuel
In the above example if the if the form with the fields name,email,and message was posting to itself then yes you would have to check the request method so that your code only executes when the form is posted and not when the page loads. if your form was posting to another page which processed the above code then you could omit the requested method check. but would still check to see if the $_POST["name"] and so on was set.
Long story short you the check the requested method so your code only executes when the page is posted.
Samuel Chun
8,890 PointsSamuel Chun
8,890 PointsI read this and re-watched the video. It looks like the method to open the contact page is GET but the method to send the form is POST.
So basically the purpose of it is to check whether or not you clicked the link "contact" (GET) or if you clicked "submit" (POST).
Please correct me if any of my thinking is wrong.
Thank you Andreas