2016-09-12 51 views
-1

我正在嘗試使用AngularJS,php和javascript構建登錄頁面。我有一個包含JSON對象的PHP頁面,包括用戶名和密碼組合。數據被訪問並存儲在$ scope.names變量中。我想要的是登錄功能能夠訪問此數組,並確定輸入到登錄表單中的登錄信息是否與在json文件中找到的任何組合匹配。

<!DOCTYPE html> 
<html > 
<style> 
table, th , td { 
    border: 1px solid grey; 
    border-collapse: collapse; 
    padding: 5px; 
} 
table tr:nth-child(odd) { 
    background-color: #f1f1f1; 
} 
table tr:nth-child(even) { 
    background-color: #ffffff; 
} 
</style> 
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> 
<body> 

<button type = "button" class = "btn btn-primary" data-toggle = "modal" data-target = "#login">Log In</button> 
<br/> 
<br/> 

<div class = "container"> 

    <h2>TTP Banking Portal</h2> 
    <ul class = "nav nav-tabs"> 
     <li class = "active"><a data-toggle = "tab" href = "#home">Home</a></li> 
     <li><a data-toggle = "tab" href = "#transfer">Transfer Funds</a></li> 
     <li><a data-toggle = "tab" href = "#reports">Generate Reports</a></li> 
     <li><a data-toggle = "tab" href = "#chat">Chat with An Agent</a></li> 
     <li><a data-toggle = "tab" href = "#email">Email your Bank</a></li> 
    </ul> 

    <div class = "tab-content"> 

     <div id = "home" class = "tab-pane fade in active"> 



<div ng-app="myApp" ng-controller="clientsCtrl" id = "login" class = "modal fade"> 

     <div class = "modal-dialog"> 
      <div class = "modal-content"> 
       <div class = "modal-header"> 
        <h3>Enter Login Information Below</h3> 
       </div> 
      <div class = "modal-body"> 
       <form> 
        <input type = "input" name = "user" placeholder = "Username" id = "Username" required> 
        <input type = "input" name = "pass" placeholder = "Password" id = "Password" required> 
        <button id = "loginbtn" type = "button" class = "btn btn-success" onclick = "login()">Log In</button> 
       </form> 
      </div> 
      <div class = "modal-footer"> 
       <button type = "button" class = "btn-warning" data-toggle = "modal" data-target = "#forgotPassword">Forget Password</button> 
       <button type = "button" class = "btn btn-primary" id = "close" name = "close" data-dismiss = "modal">Close</button> 
      </div> 
      </div> 
     </div> 
</div> 


</div> 

<script> 

var app = angular.module('myApp', []); 
    app.controller('clientsCtrl', function($scope, $http) { 
     $http.get("clients.php") 
     .then(function(response) { 
      $scope.names = response.data.records; 

     }); 
    }); 

function login() { 
    var enteredUsername = document.getElementById("Username").value; 
    var enteredPassword = document.getElementById("Password").value; 
    console.log(enteredUsername + "," + enteredPassword); 
    for (var i = 0; i < $scope.names.length; i++) { 
     console.log($scope.names[i]); 
    } 

} 


</script> 

</body> 
</html> 

謝謝。

+0

與其將用戶名/密碼組合,給客戶的,你應該檢查他們的服務器上,最好使用來自PHP框架的身份驗證解決方案。任何人都可以在devtools的網絡標籤中看到來自服務器的json響應,這使得它非常不安全。 –

+0

我工作的團隊出於某種原因想要使用此方法。所以,你知道如何做到這一點嗎? –

+0

$ scope是一個局部變量。你不能在控制器外部使用它。而是使用名爲全局變量並遍歷它。 –

回答

0

有很多方法可以做到這一點

1)控制器添加登錄功能像

var app = angular.module('myApp', []); 
    app.controller('clientsCtrl', function($scope, $http) { 
     $http.get("clients.php") 
     .then(function(response) { 
      $scope.names = response.data.records; 

     }); 
$scope.Login= function(){ 
var enteredUsername = document.getElementById("Username").value; 
    var enteredPassword = document.getElementById("Password").value; 
    console.log(enteredUsername + "," + enteredPassword); 
    for (var i = 0; i < $scope.names.length; i++) { 
     console.log($scope.names[i]); 
    } 
}; 
    }); 

HTML代碼將成爲

<form> 
<input type = "input" name = "user" placeholder = "Username" id = "Username" required> 
<input type = "input" name = "pass" placeholder = "Password" id = "Password" required> 
<button id = "loginbtn" type = "button" class = "btn btn-success" ng-click = "Login()">Log In</button> 
    </form> 

2)發送Names作爲參數傳遞Login功能

var app = angular.module('myApp', []); 
    app.controller('clientsCtrl', function($scope, $http) { 
     $http.get("clients.php") 
     .then(function(response) { 
      $scope.names = response.data.records; 

     }); 
    }); 

function login(Names) { 
    var enteredUsername = document.getElementById("Username").value; 
    var enteredPassword = document.getElementById("Password").value; 
    console.log(enteredUsername + "," + enteredPassword); 
    for (var i = 0; i < Names.length; i++) { 
     console.log(Names[i]); 
    } 

} 

現在的HTML代碼就會像

<form> 
    <input type = "input" name = "user" placeholder = "Username" id = "Username" required> 
    <input type = "input" name = "pass" placeholder = "Password" id = "Password" required> 
    <button id = "loginbtn" type = "button" class = "btn btn-success" onclick = "login({{names}})">Log In</button> 
     </form> 

3)使用Names作爲全局變量在腳本

<script> 
var Names={}; //as Global scope can use anywhere in this script scope like I used in login function 
var app = angular.module('myApp', []); 
    app.controller('clientsCtrl', function($scope, $http) { 
     $http.get("clients.php") 
     .then(function(response) { 
      $scope.names = response.data.records; 
      Names=response.data.records; 

     }); 
    }); 

function login() { 
    var enteredUsername = document.getElementById("Username").value; 
    var enteredPassword = document.getElementById("Password").value; 
    console.log(enteredUsername + "," + enteredPassword); 
    for (var i = 0; i < Names.length; i++) { 
     console.log(Names[i]); 
    } 

} 


</script> 

希望它能幫助。

+0

當我將其更改爲第一個選項時,點擊不會執行任何操作。當我將其更改爲第二個選項時,它說「名稱」未定義。 –

+0

第一個選項不產生錯誤很喜歡點擊永遠不會發生,我不知道如何使用名稱作爲一個全局變量 –

+0

要確保你的'form'標籤應在控制範圍()編輯後使用'Names'作爲全球SCOP – Paarth

1

這是AngularJS中上述代碼的純粹實現。

HTML:輸入字段的定義模型。

<form> 
    <input ng-model='user.name' type="input" name="user" placeholder="Username" id = "Username" required> 
    <input ng-model='user.password' type="password" name="pass" placeholder="Password" id="Password" required> 
    <button id="loginbtn" type="button" class="btn btn-success" ng-click="login(user)">Log In</button> 
</form> 

腳本:

var app = angular.module('myApp', []); 
    app.controller('clientsCtrl', function($scope, $http) { 

    //this is sample data for names. 
    $scope.names = [{username: 'abc', password: 'abc'},{username: 'abc1', password: 'abc1'}]; 
    $scope.user = {}; 
    $scope.login = function (user) { 
    var enteredUsername = user.name, 
     enteredPassword = user.password; 
    console.log(enteredUsername + "," + enteredPassword); 
    for (var i = 0; i < $scope.names.length; i++) { 
     if($scope.names[i].username === enteredUsername && $scope.names[i].password === enteredPassword) { 
     alert('Logged In'); 
     break; 
     } 
    } 
    } 
});