chaser
chaser

Reputation: 3195

$_SERVER['REQUEST_METHOD'] evaluates to to 'GET' by default

Been doing a bit of digging about this, but, no luck finding information

I'm trying to check whether a form has been submitted and if it is either GET or POST. So essentially I use:

if($_SERVER['REQUEST_METHOD'] == 'GET')

or

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

However, I find that if I don't submit any form, and just go to the page directly - a simple HTTP Request, the REQUEST_METHOD is GET. What gives? Is this by design? If so then I can't use the former statement to check whether a form has been submitted via GET. Seems a bit redundant...

Someone with a bit more knowledge please explain this to me, that would be appreciated. Thanks.

Upvotes: 4

Views: 9983

Answers (3)

Frank Gui
Frank Gui

Reputation: 11

I have encountered same problem and resolved by this: When the form action is set as iprofile?r=search, the request method is always GET and all input data in the form are lost. But when i set the action to iprofile/?r=search, the request method become POST.

Maybe you can also check your action URL.

Upvotes: 1

Narf
Narf

Reputation: 14752

Simply going to a page through your browser or clicking a link IS a GET request.

Upvotes: 2

meiamsome
meiamsome

Reputation: 2944

Basically most HTTP requests are GET requests.

you can use if($_POST) to check if it's a POST. (That's the array with POST data in it. All pages have $_GET set, so if($_GET) won't work to tell if it's a GET)

However, if(count($_GET)>0) will tell you if there is $_GET data.

You can have both POST and GET data though, by sending a POST request to a URL with GET data in it (i.e. http://example.unreal?GetData=4&OtherData=no)

Upvotes: 8

Related Questions