The PHP code should extract the data from the table, decode it as JSON, and then send it to AngularJS.
Angular JS redirects if login credentials are correct.
However, even if the credentials are incorrect, the user is still redirected (the true statement is always executed).
PHP code:
<?php $data = json_decode(file_get_contents("php://input")); $username = $data->username; $password = $data->password; /* * Collect all Details from Angular HTTP Request. */ require_once("connection.php"); //must read from table $connection = connectToMySQL(); //complete from here $query = "SELECT count(*) FROM tbl_admin WHERE username = '$username' AND password = '$password'"; $result = mysqli_query($connection ,$query); $row = mysqli_fetch_row($result); $count = $row[0]; if($count == 1) { echo true; } else { echo false; } ?>
AngularJS controller:
app.controller('loginCtrl', function ($scope, $http, $location) { $scope.checkCredentials = function (credentials) { $http.post('model/getAdmin.php',credentials).success(function(data){ console.log("true"); $location.path("/memberList"); }) .error(function(err){ $log.error(err); console.log("false"); }); } });
HTML form code
<form class="form-group" id="customForms" ng-controller="loginCtrl"> <label> Username </label> <input id="customFormsInput" class="form-control" ng-model="credentials.username" type="text" placeholder="Username goes here" required/> <br> <label> Password </label> <input id="customFormsInput" class="form-control" ng-model="credentials.password" type="password" placeholder="Password goes here" required/> <br> <br> <button class="btn btn-primary" type="submit" ng-click="checkCredentials(credentials)"> Submit </button> <br> {{responseMessage}} </form>
source share