• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 452
  • Last Modified:

$_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.
0
dcrudo
Asked:
dcrudo
4 Solutions
 
levyukCommented:
you can use this to check if a variable has been set



isset($_POST['cmd'])
0
 
LinuxNubbCommented:
isset is the better option, but you can also use empty()

if (!empty($_GET['cmd'])) {
..........
0
 
fehaCommented:
Here is my solution
<?php

$name="n/a";//If client don't write a name or is not required (Survey)
$email_webform=CONTACT_E_MAIL;//if client don't leave e-mail or is not required
$your_name=COMPANYNAME;//Your or Company name
$email_to=CONTACT_E_MAIL;
$email_body="";//Clean Up variable ...
$email_subject="NO SUBJECT";//if Subject is empy or not required

//validate e-mailaddress format no check with the servers
$verify_email='1';//If you have in Your form  field with name="mail" it will chek this option too
$ThankyouURL = "../Common/Display.php?message_id=14";//Your Thank You Page if Empty it will use processor message
$ErrorURL = "../Common/Display.php?message_id=15";//Your Error Page if Empty it will use processor message
//Describe all required fields and formats
$RequredURL = "../Common/Display.php?message_id=15";//Your Required Fields Page if Empty it will use processor message


//IF NOTHING SUBMITED NO PROCESSING IS REQUIRED

if (!$_POST && !$_GET ) {echo "THE FORM IS EMPTY, NOTHING TO PROCESS!";exit;}
//=======================================================================
if ( $_SERVER['REQUEST_METHOD'] == "POST") {
      foreach ($_POST as $key => $value) {
                if (eregi("r_",$key)){
                      if($value=="")
                      {

                      $key=str_replace("r_"," ",$key);
                      $key=str_replace("_"," ",trim($key));
                  if($RequredURL !=""){header ("Location:$RequredURL");exit;}
                  echo "<strong>".strtoupper($key)."</strong> IS REQUIRED<br>";
                  echo "<a href=".$HTTP_REFERER.">OK</a><br>";exit;
                 }
                }
                $key=str_replace("r_"," ",$key);
                if (eregi("subject",$key)){$email_subject=$value;}
                if (eregi("mail",$key)){$email_from=$value;
                if ($verify_email=='1')
                {
                if(!eregi('^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$', $email_from))
                {
                if($RequredURL !=""){header ("Location:$RequredURL");exit;}
                echo "<strong>".strtolower($email_from)."</strong> e-mail address format is wrong!";exit;
                }
                }
                }
                if (eregi("name",$key)){$name=$value;}
                $key=str_replace("_"," ",trim($key));
            $email_body .= ucfirst ($key) ." : ". $value . "\n";
      }
} else {
      foreach ($_GET as $key => $value) {
if (eregi("r_",$key)){
                      if($value=="")
                      {

                      $key=str_replace("r_"," ",$key);
                      $key=str_replace("_"," ",trim($key));
                      if($RequredURL !=""){header ("Location:$RequredURL");exit;}
                      echo "<strong>".strtoupper($key)."</strong> IS REQUIRED<br>";
                      echo "<a href=".$HTTP_REFERER.">OK</a><br>";exit;
                 }
                }
                $key=str_replace("r_"," ",$key);
                if (eregi("subject",$key)){$email_subject=$value;}
                if (eregi("mail",$key)){$email_from=$value;
                if ($verify_email=='1')
                {
                if(!eregi('^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$', $email_from))
                {
                if($RequredURL !=""){header ("Location:$RequredURL");exit;}
                echo "<strong>".strtolower($email_from)."</strong> e-mail address format is wrong!";exit;
                }
                }
                }
                if (eregi("name",$key)){$name=$value;}
                $key=str_replace("_"," ",trim($key));

            $email_body .= ucfirst ($key) ." : ". $value . "\n";
      }

}
//==============================================================================
if (!$email_body) {echo "THE FORM IS EMPTY, NOTHING TO PROCESS!";exit;}
//Notify Your Client
$email_body = eregi_replace("submit :","",$email_body);
$email_body = eregi_replace("submit","",$email_body);
$email_body = eregi_replace("PHPSESSID :","",$email_body);
$email_body = eregi_replace("PHPSESSID","",$email_body);
$email_body = eregi_replace("send :","",$email_body);
$email_body = eregi_replace("send","",$email_body);

$email_message=EMAIL_RESPONDER;
if(eregi("<RESPOND>",$email_message)&& $email_from !="")
{
$email_message=str_replace("<RESPOND>","",$email_message);
$email_message=str_replace("<NAME>",$name,$email_message);
$email_message=str_replace("<CONTENT>",$email_body,$email_message);
send_sys_email(CONTACT_E_MAIL, $email_from, "Re:".$email_subject, $email_message);
}

$email_body .= "---------------------------------------------\n";
$email_body .= date(CUST_DATE_FORMAT,CUSTOM_TIME)."\n";
$email_body .= "IP:".$REMOTE_ADDR."\n";
$email_body .= "Ref: " .$HTTP_REFERER. "\n".$SERVER_NAME."\n";
$email_body .= "----------------------------------------------\n";


if($email_from =="")
{
$OK=send_sys_email(CONTACT_E_MAIL, CONTACT_E_MAIL, $email_subject, $email_body);
}
else
{
$OK=send_sys_email($email_from, CONTACT_E_MAIL, $email_subject, $email_body);
}
if($OK=='true')
{
if($ThankyouURL!="")
{
header ("Location:$ThankyouURL");
}
else
{
echo "Your message has been sent<br>Thank You<br>";
echo "<a href=".$HTTP_REFERER.">OK</a><br>";
//header ("Location:$HTTP_REFERER");
}
}
else
{
if($ErrorURL !="")
{
header ("Location:$ErrorURL");
}
else
{
echo "Message could not be sent might be a problem with server connection!<br>Please try again (later)!Thank You";
echo "<a href=".$HTTP_REFERER.">OK</a><br>";
}
}
//echo "<a href=".$HTTP_REFERER.">OK</a><br>";
if($ThankyouURL!="")
{
header ("Location:$ThankyouURL");
}
else
{
//echo "<a href=".$HTTP_REFERER.">OK</a><br>";
//header ("Location:$HTTP_REFERER");
}
?>

It will process whatever input
POST or GET
for required fileds you start name for form field with r_
if it is email it will check for porpper e-mail format too ...

the main part as answer to your question is:

//=======================================================================
if ( $_SERVER['REQUEST_METHOD'] == "POST") {
      foreach ($_POST as $key => $value) {
                if (eregi("r_",$key)){
                      if($value=="")
                      {

                      $key=str_replace("r_"," ",$key);
                      $key=str_replace("_"," ",trim($key));
                  if($RequredURL !=""){header ("Location:$RequredURL");exit;}
                  echo "<strong>".strtoupper($key)."</strong> IS REQUIRED<br>";
                  echo "<a href=".$HTTP_REFERER.">OK</a><br>";exit;
                 }
                }
                $key=str_replace("r_"," ",$key);
                if (eregi("subject",$key)){$email_subject=$value;}
                if (eregi("mail",$key)){$email_from=$value;
                if ($verify_email=='1')
                {
                if(!eregi('^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$', $email_from))
                {
                if($RequredURL !=""){header ("Location:$RequredURL");exit;}
                echo "<strong>".strtolower($email_from)."</strong> e-mail address format is wrong!";exit;
                }
                }
                }
                if (eregi("name",$key)){$name=$value;}
                $key=str_replace("_"," ",trim($key));
            $email_body .= ucfirst ($key) ." : ". $value . "\n";
      }
} else {
      foreach ($_GET as $key => $value) {
if (eregi("r_",$key)){
                      if($value=="")
                      {

                      $key=str_replace("r_"," ",$key);
                      $key=str_replace("_"," ",trim($key));
                      if($RequredURL !=""){header ("Location:$RequredURL");exit;}
                      echo "<strong>".strtoupper($key)."</strong> IS REQUIRED<br>";
                      echo "<a href=".$HTTP_REFERER.">OK</a><br>";exit;
                 }
                }
                $key=str_replace("r_"," ",$key);
                if (eregi("subject",$key)){$email_subject=$value;}
                if (eregi("mail",$key)){$email_from=$value;
                if ($verify_email=='1')
                {
                if(!eregi('^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$', $email_from))
                {
                if($RequredURL !=""){header ("Location:$RequredURL");exit;}
                echo "<strong>".strtolower($email_from)."</strong> e-mail address format is wrong!";exit;
                }
                }
                }
                if (eregi("name",$key)){$name=$value;}
                $key=str_replace("_"," ",trim($key));

            $email_body .= ucfirst ($key) ." : ". $value . "\n";
      }

}


hope this helps
:-)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
RoonaanCommented:
Hi,

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

$cmd = $_REQUEST['cmd'];

-r-
0
 
andypoormanCommented:
<?
if(!isset($_GET['cmd']))
  {
       if(!isset($_POST['cmd']))
         {
            $cmd = "";
         }
       else
          {
          $cmd = $_POST['cmd'];
          }
  }
else
     {
         $cmd = $_GET['cmd'];
     }
     ?>
0
 
dcrudoAuthor Commented:
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
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now