2

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

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Yes, this is clear and working too but in this case i can ignore the whole switch thing here: i can populate these variables without switch. What's the point in that tutorial and how it can deal with both $_POST and $_GET?
I don't think, it's "RESTful" to mix get and post. So the tutorial split the requests up. So you don't have to deal with both.
REST isn't to deal with urls (as parameters)? Just because all examples are similar: GET request to /api/users – List all users, POST request to /api/users – Create a new user (Btw it's true in these examples $_GET variables aren't presented.)
The URL is the parameter as it defines the scope of the action you want (which controller to use, for example). The action itself is triggered by the type of request (get, post, ...). If this doesn't sound helpful: I recommend to look at a framework, that works that way (Yii, ...).
0

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'];

1 Comment

While what you wrote is a working solution i'd like to understand how can i handle these requests by the request object itself.
0

$_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.

1 Comment

Sorry, this is definitely not the missing piece of this puzzle.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.