Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

code to retieve value whether POST or GET

Status
Not open for further replies.

Microbe

Programmer
Oct 16, 2000
607
AU
I am passing a value to a page. Some times it is a POST sometimes it is a GET.

I can do something like
Code:
	if(isset($_POST["key"])){
		$key= $_POST["key"];
	}
	elseif(isset($_GET["key"])){
		$key= $_GET["key"];
	}
but it seems a bit ugly and clumsy.

Is there a way to grab the value by just doing something like $_["key"] ?

hmmm...maybe I should try that...

Steve
- I have fun with telemarketers
 
Microbe,

The code in your first message is the preferred and more secured one. $_REQUEST superglobal variables cosists of variables passed via either GET, POST or COOKIE. So you will never be able to control if cookie is already set by this variable name.



--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
i'd prefer your first post. if you must use $_REQUEST, then understand about GPC order and that you might also be picking up a cookie value.

alternatively you could use this as a reusable function.

Code:
$key = extractVar('key');

function extractVar($varName, $order = 'PG'){
	if (strtoupper($order) === 'GP') 
		{$one = '_GET'; 	$two = '_POST'; } 
		else 
		{$one = '_POST'; 	$two = '_GET';}
	return isset(${$one][$varName]) ? ${$one}[$varName] : (isset(${$two}[$varName]) ? ${$two}[$varName] : '');
}

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top