Error in showing data using switch case?

2

If there is data returned or received by method $ _POST, it will have the value process if there are no returned or received values, it will have the value cancel

The code states it in the following way, but there is a fatal error:

  

Fatal error: Using $ this when not in object context in C: \ xampp \ htdocs \ PayPal \ success.php on line 48

Line 48 of the error refers to the following line of code else $this->action = 'cancel';

How can I fix this error, or how to create in $_POST a true & false to get the operation of the following code:?

if(!empty($_POST)) $this->action = 'process';
else $this->action = 'cancel';
switch($this->action){
    case 'process':
        # code...
      echo "process";
        break;
    case 'cancel':
        # code...
      echo "cancel";
        break;
}
    
asked by Josues 23.10.2017 в 05:10
source

1 answer

4

A.

If you want to evaluate that the POST in general is not empty. This is one way to do it:

if ( !count($_POST)==0 )   //o !isset( $_GET )
{

    foreach ($_POST as $key => $value) 
    {
        echo $key." - ".$value;

        /*Si quieres usar un switch... case con el valor que hay en st*/
        if ($key=="st") 
        {
            switch($value)
            {
                case 'Pending':
                    # code para pending...
                    break;

                case 'Completed':
                    # code para completed...
                    break;

                default:
                    # otra cosa si hicera falta cuando no se cumplan las anteriores
                    break;
            }
        }
    }

}else{

    echo "No hay datos en el POST";
}

B.

If you receive something like this in the URL:

?action=process

or like this:

?action=cancel

Then you can retrieve the value of action in this way:

if (isset($_POST["action"])) 
{
    $action=$_POST["action"];
    switch($action)
    {
        case 'process':
            # code...
            echo "process";
            break;

        case 'cancel':
            # code...
            echo "cancel";
            break;

        default:
            # otra cosa si hicera falta cuando no se cumpla ni process ni cancel
            break;
    }
}else{

    echo "No hay ningún valor con la clave action en el POST";

}

C.

If you want to evaluate any data within the POST and, according to your comment, you want a Boolean value. You can proceed in this way.

$bolPending=false;

if (isset($_POST["st"])) {
    if ($_POST["st"]=="Pending")
    {
        $bolPending=true;
    }
}

The variable $bolPending will only be true when within the POST there is a key called st and the value of it is Pending .

    
answered by 23.10.2017 / 12:27
source