Link to home
Start Free TrialLog in
Avatar of dcrudo
dcrudo

asked on

$_POST $_GET Question in PHP ...

Dear All,

I have the following:

if($_GET['cmd'] == "")
  {
        if($_POST['cmd'] == $nil)
          {
              $cmd = "";
           }
        else
           {
           $cmd = $_POST['cmd'];
           }
  }
else
     {
          $cmd = $_GET['cmd'];
         }


Basically I would like to be able to pass variables either with post (from a form) or with get (from a string in the url) to a PHP script.

The problem is that using the condition above, if for instante the $_POST variable is empty ...the scripts gives an error like:

"Undefined index: cmd in /var/www/vhosts/demo.shironeko.com/httpdocs/cf/commands.php on line 21"

Is there a way to tell PHP that if the variable is not there...just ignore it?

Thank you!

Dave.
SOLUTION
Avatar of levyuk
levyuk

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Roonaan
Hi,

You can also just use the $_REQUEST array instead of the complicated if structure:

$cmd = $_REQUEST['cmd'];

-r-
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dcrudo
dcrudo

ASKER

Hi,

I will split the points because more than one solution was good! Actually I've used parts of all of them ;

Thx again!

Dave