简体   繁体   English

PHP 将 POST 和 GET 请求混合到一页

[英]PHP mixing POST and GET Requests to one page

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.我了解,GET 用于在不更改服务器的情况下检索页面,而 POST 用于更改服务器的事物(插入、更新、删除)。

Now I have written a page which is called with a GET request with parameter StationNr set.现在我编写了一个页面,该页面通过设置参数StationNr的 GET 请求调用。 The user can fill a form and makes a POST request to the same page with parameter Filter set.用户可以填写表单并向设置了参数Filter的同一页面发出 POST 请求。 But I don't want to miss the parameter StationNr thus I thought I give it into a hidden input field.但我不想错过参数StationNr ,因此我想我把它放到一个隐藏的输入字段中。 But then the parameter StationNr is either in the $_GET variable (first call) or in the $_POST variable (second call).但是参数StationNr要么在$_GET变量中(第一次调用),要么在$_POST变量中(第二次调用)。 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?我也不想使用$_REQUEST['StationNr']因为: 何时以及为什么应该使用 $_REQUEST 代替 $_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.我认为这是一个常见问题,但我还没有遇到过,因为我是编写 php 页面的初学者。 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:虽然您可以使用?foo=bar 在 POST 请求中推送 GET 值,但我建议改为检查请求方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM