I have this API function:
public function login($conn, $user){
$login = "SELECT username FROM login WHERE username=:user";
$execLogin = $this->conn->prepare($login);
$execLogin->bindValue(":user", $user);
$execLogin->execute();
$res = $execLogin->rowCount();
try{
return $res;
}
catch(PDOException $e)
{
return $e->getMessage();
}
}
Here is the php script when a login button is clicked:
<button id="btn-login" (click)="login()" [disabled]="!loginGroup.valid" class="btn btn-success">Login </button>
A script of the login function will run:
<?php
require_once('../api.php');
//Getting username and password from Angular
$user = $_POST['user'];
$newApi = new api();
$conn = $newApi->connection();
$res = $newApi->login($conn, $user);
?>
Here is the methods used in typescript. In the API service:
login(username, password)
{
let headerOptions = new HttpHeaders();
headerOptions.append('Access-Control-Allow-Origin', '*');
headerOptions.append('Access-Control-Request-Headers', '*');
headerOptions.append('Content-Type', 'application/json');
headerOptions.append('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,PATCH,OPTIONS');
this.credentials = {user: username, pass: password};
const httpParams = new HttpParams();
httpParams.set('user', username);
httpParams.set('pass', password);
//console.log("hi "+ this.credentials);
return this.http.post('http://dev.local/scripts/login.php', {username, password}, {
observe: 'response',
responseType: 'text',
headers: headerOptions
}).pipe(map(
res=>{
console.log(res)
},
err=>
console.log(err)
))
}
And on button click:
login(){
let user = this.loginGroup.get('username').value;
let pass = this.loginGroup.get('password').value;
this.auth.login(user, pass).subscribe(
(data)=>{
console.log(data)
},
(error)=>{
console.log(error)
}
)
}
The problem is that I always see the following error at the network tab of the console:
call to undefined variable user at line 7
It seems that the user is not send by typescript methods to PHP scripts.
EDIT