我正在嘗試使用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>
謝謝。
與其將用戶名/密碼組合,給客戶的,你應該檢查他們的服務器上,最好使用來自PHP框架的身份驗證解決方案。任何人都可以在devtools的網絡標籤中看到來自服務器的json響應,這使得它非常不安全。 –
我工作的團隊出於某種原因想要使用此方法。所以,你知道如何做到這一點嗎? –
$ scope是一個局部變量。你不能在控制器外部使用它。而是使用名爲全局變量並遍歷它。 –