recover values independently of GET or POST used

recover values independently of GET or POST used

am 15.09.2009 04:41:24 von buzon

I am using some expressions to load the value of a parameter when
called, independently if used GET or POST method, using something like:

$mydata = (isset($_GET["mydata"])) ? $_GET["mydata"] :
(isset($_POST["mydata"])) ? $_POST["mydata"] : 0;
echo $mydata;
?>

The problem occurs with above code if I call it like:
thispage.php?mydata=something , the answer is null (not even '0').

But If I change it to:

$mydata = (isset($_POST["mydata"])) ? $_POST["mydata"] :
(isset($_GET["mydata"])) ? $_GET["mydata"] : 0;

It works, but only with GET method, not post.


What is going on?

Everytime must be an if/elsif/else routine? What is the origin of the problem?


TIA,

Joal


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: recover values independently of GET or POST used

am 15.09.2009 05:01:01 von Trevor Gryffyn

Some weird stuff happens when you try to chain ternary operations. Not
awake enough to explain it better right now.

What it sounds lik eyou're looking for is $_REQUEST. This will give you
the correpsonding GET or POST value (as well as cookie, environment,
etc.. google it).

It's probably good to try to structure things so you have a predictable and
controllable way so you know whether you're going to be getting GET or
POST data. Some cases required 'either' but in general, one or the other
should be good enough if your code is structured properly.

-TG

----- Original Message -----
From: buzon@alejandro.ceballos.info
To: php-db@lists.php.net
Date: Mon, 14 Sep 2009 19:41:24 -0700
Subject: [PHP-DB] recover values independently of GET or POST used

> I am using some expressions to load the value of a parameter when
> called, independently if used GET or POST method, using something like:
>
> > $mydata = (isset($_GET["mydata"])) ? $_GET["mydata"] :
> (isset($_POST["mydata"])) ? $_POST["mydata"] : 0;
> echo $mydata;
> ?>
>
> The problem occurs with above code if I call it like:
> thispage.php?mydata=something , the answer is null (not even '0').
>
> But If I change it to:
>
> $mydata = (isset($_POST["mydata"])) ? $_POST["mydata"] :
> (isset($_GET["mydata"])) ? $_GET["mydata"] : 0;
>
> It works, but only with GET method, not post.
>
>
> What is going on?
>
> Everytime must be an if/elsif/else routine? What is the origin of the
problem?
>
>
> TIA,
>
> Joal
>
>
> --
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: recover values independently of GET or POST used

am 15.09.2009 05:25:36 von dmagick

buzon@alejandro.ceballos.info wrote:
> I am using some expressions to load the value of a parameter when
> called, independently if used GET or POST method, using something like:
>
> > $mydata = (isset($_GET["mydata"])) ? $_GET["mydata"] :
> (isset($_POST["mydata"])) ? $_POST["mydata"] : 0;
> echo $mydata;
> ?>
>
> The problem occurs with above code if I call it like:
> thispage.php?mydata=something , the answer is null (not even '0').

You probably need extra ()'s around the post check:

$mydata = (isset($_GET["mydata"])) ? $_GET["mydata"] :
((isset($_POST["mydata"])) ? $_POST["mydata"] : 0);

Though that's hard to read - I'd suggest going back to basics because
when you revisit this code in a few months time, are you going to
understand it?

--
Postgresql & php tutorials
http://www.designmagick.com/


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Summary: recover values independently of GET or POST used

am 15.09.2009 14:49:02 von buzon

--Apple-Mail-2--166163818
Content-Type: text/plain;
charset=US-ASCII;
format=flowed;
delsp=yes
Content-Transfer-Encoding: 7bit


Thank you TG and Chris.

Using $_REQUEST or something like $parameter = array_merge($_GET,
$_POST); makes me a little noise; so preffer using extra ()'s,
changing to $mydata = (isset($_GET["mydata"])) ? $_GET["mydata"] :
((isset($_POST["mydata"])) ? $_POST["mydata"] : 0); and include a
little comment above.

It works, thank you

J. Alejandro Ceballos Z.

w: http://alejandro.ceballos.info
e: buzon@alejandro.ceballos.info
m: +52 (33) 1411.6079




--Apple-Mail-2--166163818--