0

I have a basic form that post to PHP file.

<form action="index.php" method="POST">
<input name="operation" id="operation" placeholder="operation" />
<br>
<input id="name" name="name" placeholder="Name" />
<br>
<input id="email" name="email" placeholder="Email"/>
<br>
<input id="password" name="password" placeholder="Password"/>
<br>
<button type="submit" >POST</button>
</form>

problem is the operation is posting NULL or empty via the index file below. I am using the basic php://input to get encoded via json.

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    $data = json_decode(file_get_contents('php://input'));

    if(isset($data -> operation)){

      $operation = $data -> operation;
      echo $operation;
       if(!empty($operation)){

       }else{

           //$operation is empty ...

       }

     }else{

          //$operation is not set ...

     }

  }

However echoing the file_get_contents('php://input') displays the correct values from the posted form.

Any reason why the $operation return is always empty?

6
  • its a text field and is used like so ... if($operation == 'register'){ //...} Commented Feb 5, 2017 at 0:32
  • 1
    debugging: var_dump($data->operation, $operation); to see what it actually contains? Commented Feb 5, 2017 at 0:34
  • both var_dump($data->operation); //returns NULL var_dump(json_decode(file_get_contents("php://input"))); //returns NULL yet i physically type a value into the form input ... Commented Feb 5, 2017 at 0:36
  • more debugging: var_dump($_POST, $_POST['operation']); to see if it is in there? Commented Feb 5, 2017 at 0:38
  • Yes both var_dump(file_get_contents('php://input')); var_dump($_POST, $_POST['operation']); output the POST Data ... Commented Feb 5, 2017 at 0:40

1 Answer 1

2

Your data should be prepared before convert to Json format Try this code :) I test this Code, it work. Good Luck



    if ($_SERVER['REQUEST_METHOD'] == 'POST')
{

    $data = file_get_contents('php://input');
    $data = str_replace('=','":"',$data);
    $data = str_replace('&','","',$data);
    $data = '{"'.$data.'"}';

    $data = json_decode($data);

    if(isset($data->operation)){

      $operation = $data -> operation;
      echo $operation;
       if(!empty($operation)){
            echo "NOT EMPTY";
       }else{

            echo "IS EMPTY";
           //$operation is empty ...

       }

     }else{
            echo "NO OPERATION";
          //$operation is not set ...

     }

  }


Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.