开发者

PHP mixing POST and GET Requests to one page

开发者 https://www.devze.com 2023-03-16 18:35 出处:网络
After reading: When do you use POST and when do you use GET? Mixing GET with POST - is it a bad practice?

After reading:

  • When do you use POST and when do you use GET?
  • Mixing GET with POST - is it a bad practice?

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'];开发者_StackOverflow社区

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!


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') { ... }


just use

<form method="post" action="script.php?get=variables">
 <input name="your_inputs" />
</form>


Correct Syntax Would Be:

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

精彩评论

暂无评论...
验证码 换一张
取 消