简体   繁体   中英

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.

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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