PHP代码需要从表中获取数据,将其解码为JSON,然后将其发送到AngularJS.
如果登录凭据正确,Angular JS会重定向.
但是,即使凭据不正确,仍然会重定向用户(true语句始终通过).
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控制器:
app.controller('loginCtrl', function ($scope, $http, $location) {
/*
* This method will be called on click event of button.
* Here we will read the email and password value and call our PHP file.
*/
$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表单代码
Username
Password
Submit
{{responseMessage}}