1

After reading:

I understand, that GET is used to retrieve a page without changing the server and POST is used for things (insert, update, delete), that change the server.

Now I have written a page which is called with a GET request with parameter StationNr set. The user can fill a form and makes a POST request to the same page with parameter Filter set. But I don't want to miss the parameter StationNr thus I thought I give it into a hidden input field. But then the parameter StationNr is either in the $_GET variable (first call) or in the $_POST variable (second call). I can do something like:

if (isset($_GET['StationNr']))
    $snr = $_GET['StationNr'];
else if (isset($_POST['StationNr']))
    $nr = $_POST['StationNr'];

But I don't like this. Also I don't want to use $_REQUEST['StationNr'] because of: When and why should $_REQUEST be used instead of $_GET / $_POST / $_COOKIE?

I think this is a common issue but I haven't faced it yet because I'm a beginner in writing php pages. How did you solve this problem?

Thanks!

Community
  • 1
  • 1
Christian Ammer
  • 7,464
  • 6
  • 51
  • 108
  • It might not be the cleanest design, put you can certainly fire a POST request to an address to which you append a `?param=value&...` string to have both GET and POST. – Kerrek SB Jul 02 '11 at 20:58
  • The `$_REQUEST` badmouthing is unwarranted. With older setups it was subject to cookie fixation, which is often a usability, but not a security issue. Nevertheless it's intended as shortcut for exactly that (GET/POST input parameters as aliases). – mario Jul 02 '11 at 21:19

3 Answers3

3

Although you can use ?foo=bar to push GET values in a POST request, I'd suggest checking the request method instead:

if($_SERVER['REQUEST_METHOD'] == 'POST') { ... }
Jani Hartikainen
  • 42,745
  • 10
  • 68
  • 86
1

just use

<form method="post" action="script.php?get=variables">
 <input name="your_inputs" />
</form>
genesis
  • 50,477
  • 20
  • 96
  • 125
  • Ahh I see, I gave me this answer with a link in my question :-) Consequently the page is requested by a POST but I can also query the $_GET variables? – Christian Ammer Jul 02 '11 at 21:10
-2

Correct Syntax Would Be:

if (isset($_GET['StationNr'])) {   
$snr = $_GET['StationNr'];
}else if (isset($_POST['StationNr']))    
$nr = $_POST['StationNr']; 
}
rackemup420
  • 1,600
  • 2
  • 15
  • 37