我有一个发布到PHP文件的基本表单。
<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')会从发布的表单中显示正确的值。
为什么$ operation返回始终为空?
答案 0 :(得分: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 ...
}
}