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 Simple PHP Application Adding a Contact Form Checking the Request Method

$_SERVER[REQUEST_METHOD"]

Hello,

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

Thanks!

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)

Thanks!