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 Basic PHP Website (2018) Adding a Basic Form Checking the Request Method

jinhwa yoo
jinhwa yoo
10,042 Points

help me plz

help me to understand specifically..

how this code connected.. I feel confused ....
each code have a meaning of something...

but still can not realize at all....

why put this part of "if($_SERVER[]"REQUEST_METHOD"] =="POST"" ???? why put "location:suggest.php?status=thanks" ???? what about this part <?php if(isset($_GET["status"]) && $_GET["status"] == "thanks")??? help me...

<?php

if($_SERVER[]"REQUEST_METHOD"] =="POST"{ $name = $_POST["name"]; $email = $_POST["email"]; $details = $_POST["details"];

echo "<pre>"; $email_body=""; $email_body .= "Name " . $name . "\n"; $email_body .= "Email " . $email . "\n"; $email_body .= "Details " . $details . "\n"; echo $email_body; echo "</pre>";

//To Do: Send Email header("location:suggest.php?status=thanks"); }

$pageTitle = "Suggest a Media Item"; $section = "suggest";

include("inc/header.php"); ?>

<div class="section page"> <div class= "wrapper"> <h1>Suggest a Media Item</h1> <?php if(isset($_GET["status"]) && $_GET["status"] == "thanks"){ <p>Thanks for the email! I’ll check your suggestion shortly!</p>;

} else {?> <p>If you think there is something I’ missing, let me know! complete the form to send me an email!</p> <form method = "post" action="suggest.php"> <table> <tr> <th><label for="name">Name</label></th> <td><input type="text" id="name" name="name" /></td> </tr> <tr> <th><label for="email">Email</label></th> <td><input type="text" id="email" name="email" /></td> </tr> <tr> <th><label for="details">Suggest Item Details</label></th> <td><textarea name="details" id="detials"></textarea></td> </tr> </table> <input type="submit" value="send" /> </form>
<?php } ?> </div> </div>

<?php include("inc/footer.php"); ?>

Simon Coates
Simon Coates
28,694 Points

can you take a look at the Markdown cheatsheet link before submitting code?

Simon Coates
Simon Coates
28,694 Points
header("location:suggest.php?status=thanks");

this is a redirect to suggest.php. This means that a user makes a GET request to suggest.php, where a parameter of status is used to indicate that the page should thank the user.

Simon Coates
Simon Coates
28,694 Points
if($SERVER["REQUEST_METHOD"] =="POST")

this is meant to confirm that the page is running in a POST context (this will happen if the page is accessed via a <form> submit, since the form uses:

 method = "post"

). If submitted by form, the script will attempt to use the submitted data. If not submitted by form, then the page will display the form to the user.