php通过JSON响应发布变量

时间:2016-05-02 10:31:48

标签: php json

有没有人有想法如何通过包含JSON的相同php响应将POST值发送到php文件?

到目前为止我所做的是将用户名和密码发送到服务器端身份验证。但它会触发字段丢失的错误。所以我想在控制台的服务器端检查收到的数据。

  if(isset($_POST['u_name']) && isset($_POST['u_pass'])){

       $username = $_POST['u_name'];
       $pass = $_POST['u_pass'];


  } else {
       // required field is missing
       $response["success"] = 0;
       $response["message"] = "Required field(s) is missing" + $_POST['u_name'] + $_POST['u_name']; // <--- this is my line 48
       // echoing JSON response
       echo json_encode($response);

  }  

但是我收到了这样的错误

   <br />
   <b>Notice</b>:  Undefined index: u_name in <b>C:\xampp\htdocs\TestCordova\login_check.php</b> on line <b>48</b><br />
   <br />
   <b>Notice</b>:  Undefined index: u_name in <b>C:\xampp\htdocs\TestCordova\login_check.php</b> on line <b>48</b><br />
   {"success":0,"message":0}

1 个答案:

答案 0 :(得分:0)

试试这个

<?php
if(isset($_POST['u_name']) && isset($_POST['u_pass'])){
   $username = $_POST['u_name'];
   $pass = $_POST['u_pass'];
} else {
   // required field is missing
   $response["success"] = 0;
   $string = "";
   if(!isset($_POST['u_name'])
    $string = "User name";
   if(!isset($_POST['u_pass'])
    $string.= " Password";

   $response["message"] = "Required field(s) is missing ".$string ; // <--- this is my line 48
   // echoing JSON response
   echo json_encode($response);

}  
?>