I am trying to make a login for my app with Ionic. So im sending data from ionic to PHP. Im not sure what im doing wrong.
HTML form part:
<ion-item id="roundedInput">
<ion-label><ion-icon name="person" color="white"></ion-icon></ion-label>
<ion-input placeholder="Gebruikersnaam" name="gebruikersnaam" #gebruikersnaam></ion-input>
</ion-item>
<ion-item id="roundedInput">
<ion-label><ion-icon name="lock" color="white"></ion-icon></ion-label>
<ion-input type="password" placeholder="Wachtwoord" name="wachtwoord" #wachtwoord> </ion-input>
</ion-item>
login.ts :
let data = {
gebruikersnaam: this.gebruikersnaam.value,
wachtwoord: this.wachtwoord.value
};
let loader = this.loading.create({
content: 'Processing, please wait…',
});
loader.present().then(() => {
this.http
.post('https://rexmediahosting.nl/ionic/login.php', JSON.stringify(data))
.map(response => response.json()).subscribe(response => {
console.log(response)
loader.dismiss()
if (response =='success') {
let alert = this.alertCtrl.create({
title: 'Succesvol ingelogt',
subTitle: (response),
buttons: ['OK']
});
alert.present();
} else {
let alert = this.alertCtrl.create({
title: 'error',
subTitle:(response),
buttons: ['OK']
});
alert.present()
}
});
});
and the final part login.php: I have the login.php file "Acccess-Control-Allow-Origin" stuff, so CORS isn't the problem
// Create connection
$mysqli = new mysqli($localhost, $username, $password, $database);
// Check connection
if ($con->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
$data = file_get_contents('php://input');
if (isset($data)){
$request = json_decode($data);
$gebruikersnaam = $request->gebruikersnaam;
$wachtwoord = $request->wachtwoord;
$gebruikersnaam = mysqli_real_escape_string($mysqli, $gebruikersnaam);
$wachtwoord = mysqli_real_escape_string($mysqli, $wachtwoord);
$gebruikersnaam = stripslashes($gebruikersnaam);
$wachtwoord = stripslashes($wachtwoord);
// if ($wachtwoord != ""){
// echo json_encode($wachtwoord);
// } else {
// echo json_encode('no wachtwoord');
// }
// Maak Query aan
$query = "SELECT * FROM Gebruikers WHERE gebruikersnaam='$gebruikersnaam' AND wachtwoord='$wachtwoord'";
// Voer Query uit
$result = mysqli_query($con, $query);
// Controlleer of de login correct was
if (mysqli_num_rows($result) != 0 ){
// login correct
$response = "succes";
} else {
// login incorrect
$response = "fail";
}
// Encode $response naar JSON
echo json_encode($response);
}
image from the database structure : table name is 'Gebruikers'
So basically, Im trying to make a login screen for my 'app'. for that i'm using phpmyadmin as db. So i try to pass data from ionic to an external php file on a server, to get a response. To check if my inputs are the same as in the database. When I put the correct values (same as in the database), I still get a 'fail' response. So it goes wrong somewhere, but I don't know where. I hope you guys can help me!!