tags:

views:

281

answers:

6

Hello,

I would like to make a variable $find equal to $_POST['find'] if the user is landing on the page after using the POST method, but on the same page make $find equal to $_GET['find'] if the user is landing on the page after using the GET method.

How do I do that?

Thanks in advance,

John

$find = $_GET['find'];

$find = $_POST['find'];
+10  A: 

You should use the $_REQUEST global variable. It contains all the data from both $_GET and $_POST.

Alternatively, you could check the request method.

$find = ($_SERVER['REQUEST_METHOD'] == 'GET') ? $_GET['find'] : $_POST['find'];
Chris Thompson
A: 

Use $_REQUEST['find'] to get the combined information from $_GET, $_POST and $_COOKIE.

$_REQUEST returns an associative array that by default contains the contents of $_GET, $_POST and $_COOKIE (You can change the default contents with the php.ini "request_order" directive)

Wil
A: 
if(isset($_GET['find'])){
    $find = $_GET['find'];
}elseif(isset($_POST['find'])){
    $find = $_POST['find'];
}else{
    $find = null;
}
// do stuff with $find;
inkedmn
+3  A: 

The $_REQUEST variable contains the contents of $_GET, $_POST and $_COOKIE where the variables_order option defines the order in which the variables are read and overwrite already existing contents.

So you can either use $_REQUEST['find'] (considering that a cookie value for find will override both POST and GET). Or you implement it on your own:

if (isset($_GET['find'])) {
    $find = $_GET['find'];
}
if (isset($_POST['find'])) {
    $find = $_POST['find'];
}

With that code $find will be $_GET['find'] if it exists unless there is a $_POST['find'] that will override the value from $_GET['find'].

Gumbo
+1  A: 

Literally what you're asking for is:

if ($_SERVER['REQUEST_METHOD']=='POST') {
   $find = $_POST['find'];
} else {
   $find = $_GET['find'];
}

Alternatively, you can use:

   $find = $_REQUEST['find'];

$_REQUEST is a combination of $_GET, $_POST and $_COOKIE.

grahamparks
A: 

If you don't care which method was used then just use the $_REQUEST super global.

If it matter which method was used:

$find = isset($_GET['find']) ? $_GET['find'] : false;
if ($find === false) {
   $find = isset($_POST['find']) ? $_POST['find'] : '';
   // do a POST-specific thing
}
else {
    // do a GET-specific thing
}
Karim