2017-02-05 32 views
0

我有張貼到PHP文件中的基本形式。PHP POST是空

<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> 

問題是操作通過下面的索引文件發佈NULL或空。 我正在使用基本的php://輸入來通過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 ... 

    } 

    } 

但是,回顯file_get_contents('php:// input')將顯示來自發布表單的正確值。

任何理由$操作的回報始終是空的?

+0

它的文本字段,並使用像這樣......如果($操作== '註冊'){// ...} – BENN1TH

+1

調試:'的var_dump($數據 - >運行, $操作);'看看它實際上包含什麼? –

+0

var_dump($ data-> operation); //返回NULL var_dump(json_decode(file_get_contents(「php:// input」))); //返回NULL但我物理輸入一個值到所述形式的輸入... – BENN1TH

回答

1

你的數據應該做好準備之前轉換成JSON格式試試這個代碼:) 我測試此代碼,它的工作。 好運

 


    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 ... 

    } 

    } 

 
+0

謝謝@AMEACHAQ – BENN1TH