2

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}
Thush-Fdo
  • 506
  • 1
  • 9
  • 28
  • ... Well, you are quiet literally checking if the values are there, and if not, then throwing a error, why are you surprised that you get a error? – Epodax May 02 '16 at 10:33
  • `else` is executing because `$_POST['u_name']` is not there. So how can you except the value – urfusion May 02 '16 at 10:34
  • 2
    you cannot use $_POST['u_name'] in else because if it goes to else that mean $_POST['u_name'] isn't set – Madhawa Priyashantha May 02 '16 at 10:34
  • @epodax I'm not suprised. I just want to check what are those values I'm sending. Hope you got what I really want from this? – Thush-Fdo May 02 '16 at 10:35
  • @ast-snail is there anyway I can check them? – Thush-Fdo May 02 '16 at 10:35
  • 1
    Just use "Required field(s) is missing" OR "Username and Password fields missing". You can not use same variable in else condition of isset. – RJParikh May 02 '16 at 10:36
  • Do you understand what `isset()` does? – RiggsFolly May 02 '16 at 10:39
  • @riggsfolly It just check whether the $_POST() variable is set for a value....? Am I correct? – Thush-Fdo May 02 '16 at 10:40
  • Yes, so if `isset($_POST['u_name'])` returns false you cannot then use `$_POST['u_name']` in an error message in the ELSE! Apart from the obvious thing that you already know it does not have a value, **it does not actually exist** – RiggsFolly May 02 '16 at 10:42
  • @riggsfolly Got it....... – Thush-Fdo May 02 '16 at 10:43
  • 1
    So as @RuchishParikh tried to say, just output a generalised error message saying `"Username and Password fields missing"` – RiggsFolly May 02 '16 at 10:45
  • 2
    **Side Note:** `isset()` can take more than one parameter so your IF can be coded as `if ( isset($_POST['u_name'], $_POST['u_pass']) ) {` – RiggsFolly May 02 '16 at 10:46

1 Answers1

0

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

}  
?>
Mohammedshafeek C S
  • 1,916
  • 2
  • 16
  • 26