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

Erick Bongo
Erick Bongo
8,539 Points

The use of $_SERVER["REQUEST_METHOD"]

In the tutorial why did we use this

if ($_SERVER["REQUEST_METHOD"] == "POST")

Instead of this

if (ISSET($_POST["SUBMIT"])) 

Am not clear on the difference between the two if someone could explain the reason and logic behind both for me.

3 Answers

Blayne Holland
Blayne Holland
19,321 Points

My guess is that if for whatever reason $_POST['submit'] is null the form will still be procesed. I've always wonders this too to be honest.

Erick Bongo
Erick Bongo
8,539 Points

I think I see what your saying. So using $_POST["submit"] is only checking that single input within any given form, where as the request method will check them all.

Randy Hoyt, could you shed some light on this please and give a practical example of when one may be used over the other and the reasons why ?

Erick Bongo
Erick Bongo
8,539 Points

Ok I just did some tests. If I use the $_POST["submit"] then hit return when filling out the form it submits the form which isn't very good.

Also if I use var_dump[$_POST] nothing gets returned, does this mean the values in the form aint being stored in the variables when using $_POST["submit"] ??