1
export class LoginComponent 
{
     getdata : string;         
      public data;         
      username : any ;          
       password : any ; 

    constructor(private http: Http){}   

    login() {        
     var headers= new Headers({'Content-Type' : 'application/x-www-form-urlencoded '});        
    var body = JSON.stringify({    
       user : this.username,
       pwd : this.password,    
 })        
  this.http.post('.../pos_system/Widgets/Login.php',body, {
  headers:headers; })
   .map(res => res.json())
   .map(res => {
         if(res.success)
         {
            this.msg="Login Complete";
         }
         else{
             this.msg1="username and password is wrong";
         }
    })
      .subscribe(
        data =>this.getdata = JSON.stringify(data),
        err => console.error(err),
        () => console.log('done'));
    }
}

This is my Angular2 part here i m getting JSON data from php file in res.Now I want to use this response in my angualr2 part.I want to use display username of the user which is in json data.so how to do that in angular2?

1 Answer 1

2

it's simple just make this change, for Example this.msg=res.username; this will display username in message. note: you need to do this in php file.

$data=array();

    $df=json_decode(file_get_contents("php://input"));
    $nam=$df->user;
    $pws=$df->pwd;

    $select=mysql_query("SELECT * FROM userData WHERE username='$nam' AND password='$pws'")or mysql_error();

    $sql=mysql_num_rows($select);
    if($sql>0)
    {
        while($row=mysql_fetch_array($select))
        {
            $data['success']=true;
            $user=$row['username'];
            $data['username']=$user;

        }
    }
    echo json_encode($data);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.