douyan3478 2016-05-02 10:31
浏览 28

php通过JSON响应发布变量[重复]

Does anyone has an Idea how to get the POST value send to the php file through the same php response wrapped with JSON?

What I did so far was to send username and password to server side authentication. But it triggers an error of field missing. So I want to check the received data at the server side at the console.

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

  }  

But I 'm getting an error like this

   <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}
</div>
  • 写回答

1条回答 默认 最新

  • dtrb96410 2016-05-02 13:14
    关注

    Try this

    <?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);
    
    }  
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line