Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP Build a Simple PHP Application Adding a Contact Form Checking the Request Method



Can you please tell me what $_SERVER["REQUEST_METHOD"] means and does?


2 Answers

$_SERVER is an array containing information such as headers, paths, and script locations. One of the elements in the array is the Request method.

There are 2 types of request methods, GET and POST. GET submits data through the URL when submitting the form while POST submits it with the HTTP request. More information about GET and POST can be found here here .

The Request Method element stores which type of request method is used when submitting the form. If you submit the form with POST method its value will be POST and if you submit with GET its value will be GET. By default, the request method is GET. In the project you use POST to submit forms. When a form is submitted, the value of the Request Method element changes form GET to POST. So if the value of the element is POST, we know that the form has been submitted and we can now display something else to the the user (a thank you or a confirmation message).

Hello Ivan,

Thanks, I totally got it. Just one thing, how would I know when to use $_SERVER() and "REQUEST_METHOD".

Let me try to answer my own question:

Should we use $_SERVER("RESQUEST_METHOD) when we want to get the values of a form all at once. Usually, when we want an individual value, we write $name = $_POST['name'], but I suppose by checking to see if( $_SERVER("RESQUEST_METHOD=="POST"), wait we are just checking, it doesnt really mean that $_SERVER will get all the values of a form with a post method. Ok, I am not sure why and when to use $_SERVER("RESQUEST_METHOD) . If you could please shed some light on the purpose of $_SERVER("RESQUEST_METHOD)