PHP REST variables missing – Education Career Blog

In this tutorial‘s processRequest method:

...
switch ($request_method){
case 'get':
        $data = $_GET;
        break;
case 'post':
        $data = $_POST;
        break;
 ...

looks like $_GET variables are ignored when $_POST happens (at least this happening in my test setup – not the same script but idea is similar).

My test case:

//URL: `example.com/?iam=get`
//HTML:
<form action="?iam=get" method="post">
    <input type="text" name="textinput" />
    <input type="submit" />
</form>

Printing $data on request gives me:

Array ( iam => get ) //Opening the page without submit
Array ( textinput => angry fabrik ) //Submitting the form

(Because of form’s action, url isn’t changing but $_GET variable iam is missing.)

I’m often using $_GET and $_POST variables mixed (AJAX requests, handling forms, etc.) but now i’m sure about i’m overlooking something. Where’s my misunderstanding?

Thanks in advance,
fabrik

,

The request method is always post here, so the switch comand ignores the “get”-part.

Try

...
switch ($request_method){
case 'get':
        $data_get = $_GET;
        break;
case 'post':
        $data_post = $_POST;
        $data_get = $_GET;
        break;
 ...

and use the new variables.

,

In the code you’ve provided above $data is populated with either $_GET or $_POST depending on the request method.

If you submit a form $_GET is ignored. But in either case $_GET will still contain the "iam” variable and you can access it any time you want with something like

$iam = $_GET'iam';

,

$_REQUEST might be what you are looking for. $_REQUEST Manual. If you don’t want to trust it (it also stores $_COOKIE) you can write your own merger for $_GET and $_POST.

Leave a Comment