2017-05-25 70 views
0

我正在使用angularJS和php在后端的应用程序,我想在我的应用程序中实现验证,并且我尝试了下面粘贴的代码,但它不会生成任何错误,任何消息,当我尝试输入用户名和密码时,无论用户输入什么内容,都会转到view'admin,这意味着它不检查用户和密码。在'client'表的数据库中,我有: NomClient和MDP(密码)。如何在AngularJS中实现验证

的login.html

<ion-content class="padding" ng-controller="loginCtrl"> 
<div class="list list-inset" > 
<label class="item item-input"> 
     <input type="text" placeholder="nom" required="" ng-model="NomClient"> 
</label> 
<label class="item item-input"> 
     <input type="password" placeholder="Password" ng-model="mdp"> 
</label> 
    <button class="button button-block button-positive" ng-click="submit()">Login</button>  
</ion-content> 

app.js

app.controller('loginCtrl', function($scope,$state,$http){ 
    $scope.submit= function(){ 
const url = 'http://localhost/deb/login.php'; 
const postBody = { 
    NomClient: $scope.NomClient, 
    mdp: $scope.mdp 
}; 

$http.post(url, postBody).then(data => { 
    $location.path('/admin'); 
}); 

};

的login.php提前

<?php 

$data = json_decode(file_get_contents("php://input")); 

$connect = mysqli_connect("localhost", "root", "", "tem"); 


$response['status'] = 0; 
$response['message'] = ''; 
$NomClient=mysqli_real_escape_string($connect, $data->NomClient); 
$mdp=mysqli_real_escape_string($connect, $data->mdp); 


$query = 'SELECT * FROM `client` WHERE NomClient = "'.$NomClient.'" AND mdp= "'.$mdp.'"'; 


if(mysqli_connect_errno()){ 
    $response['status'] = 0; 
    $response['message'] = "Failed to connect to MySQL: ".mysqli_connect_error(); 
    echo jsone_encode($response);exit; 
} 

$result = mysqli_query($connect, $query); 
$rowcount=mysqli_num_rows($result); 
if($rowcount>0){ 
    $response['status'] = 1; 
    $response['message'] = 'Login successful'; 
} 
else{ 
    $response['status'] = 0; 
    $response['message'] = 'Invalid username of password'; 
} 

echo json_encode($response);exit; 
?> 

感谢。

回答

0

首先,你没有发送任何数据到你的PHP登录处理程序。您目前在AngularJS中的提交功能是发出取得请求而不是发布。

$scope.submit功能应该是这个样子(这不是首选方法,但可能是最简单的一个鉴于你目前的设置):

app.js

$scope.submit = function() { 

    const url = 'http://localhost/deb/login.php'; 
    const postBody = { 
     NomClient: $scope.NomClient, 
     mdp: $scope.mdp 
    }; 

    $http.post(url, postBody).then(data => { 
     // do whatever you need with the login data (token?) 
    }); 

}; 

其次,当如果你的PHP脚本获取了POST数据?

您需要在PHP脚本的开头添加一些内容以检索POST数据。

$data = json_decode(file_get_contents('php://input'), true); 
+0

谢谢你,我做了什么你syggested我编辑的代码,但仍然没有结果 – SalamSalam

+0

你能提供一些错误日志或至少说明你在哪里卡住? –

+0

我没有错误,当我期待代码,也没有消息,当我点击提交时,没有任何改变,我仍然在同一页 – SalamSalam