@ayodele2242 wrote:
I want to do a login verification, using PHP, MySQL and JSON and storing the return data into a localStorage.
I believe my PHP and JSON part is well taking care of.
If the user enters the username and password it needs to check the user table using PHP and MySQL and check if it is valid. If username and password matches then the status = valid.
In my JSON I added the valid status, the json data was returned from php but the line
if (this.data.loginStatus == 'Valid') { }
in login.ts is not been executed. What could be the cause?
Here are that values been returned:
Here are the values been returned:
{"data":[ {"loginStatus":"Valid", "CustomerName":"James Fred", "Mobile":null, "Email":"fred48@gmail.com", "CustomerID":"3" } ] }
auth-service.ts
login(credentials) { return new Promise((resolve, reject) => { this.http.post(apiUrl + '/login.php', JSON.stringify(credentials)) .subscribe(res => { resolve(res.json()); }, (err) => { reject(err); }); }); }
Login.ts
doLogin() { if(this.loginData.LoginID =='' || this.loginData.Password ==''){ //presentToast(msg) { let toast = this.toastCtrl.create({ message: "Please enter your username and password", duration: 3000, position: 'bottom', dismissOnPageChange: true }); toast.onDidDismiss(() => {}); toast.present(); //} }else{ this.showLoader(); this.authService.login(this.loginData).then((result) => { this.data = result['data']; console.log("Result: "+ this.data); if (this.data.loginStatus == 'Valid') { localStorage.setItem('loginStatus', this.data.loginStatus); localStorage.setItem('CustomerName', this.data.CustomerName); localStorage.setItem('Mobile', this.data.Mobile); localStorage.setItem('Email', this.data.Email); localStorage.setItem('CustomerID', this.data.CustomerID); this.navCtrl.setRoot(MyApp); this.loading.dismiss(); } else { document.getElementById('err-span').style.display = "block"; this.loading.dismiss(); } }, (err) => { this.loading.dismiss(); this.presentToast(err); }); } }
Login.php
$data = file_get_contents("php://input"); if (isset($data)) { $request = json_decode($data); $LoginID = $request->LoginID; $Password = $request->Password; } $LoginID = $mysqli->real_escape_string($LoginID); $Password = encryptIt($Password); $result = mysqli_query($mysqli,"SELECT * FROM customer_login WHERE email = '$LoginID' AND password = '$Password'"); $count = mysqli_num_rows($result); $response = array(); //the array that will hold the titles and links $data = array(); if($count < 1) { $data[] = "Invalid details entered. Please try again."; }else{ //create an array while($row = mysqli_fetch_array($result)) { $loginStatus = 'Valid'; $custname = $row['last_name'] .' '.$row['first_name']; $mobile = $row['mobile']; $email = $row['email']; $id = $row['id']; //each item from the rows go in their respective vars and into the posts array $data[] = array('loginStatus'=> $loginStatus, 'CustomerName'=> $custname, 'Mobile'=> $mobile, 'Email'=>$email, 'CustomerID'=>$id); } } $response['data'] = $data; echo json_encode($response);
Posts: 1
Participants: 1