一個簡單的登錄表單我有一個登錄表單是這樣的: 我有3個文件:index.html
,login.php
,custom.js
創建AJAX,PHP,jQuery的
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<section>
<div id="message"></div>
<form id="loginform" method="POST">
<p>Username: <input type="text" id="username" name="username" size=25 />
</p>
<p>Password: <input type="password" id="password" name="password" size=25 />
</p>
<button id="login" type="submit" name="submit">Login</button>
</form>
</section>
</body>
</html>
custom.js
看起來是這樣的:
$('#login').click (function() {
$.ajax({
type: "POST",
url: 'login.php',
data: $('#loginform').serialize(),
dataType: 'json',
success: function(data) {
//alert(data.message);
// print into (<div id="message"></div>) on file html.
},
});
return false;
});
login.php
看起來這樣:
$users = array(
0 => array(
"username" => "admin",
"password" => "admin",
),
1 => array(
"username" => "demo",
"password" => "demo",
),
);
請檢查下面我的代碼:
for ($i=0; $i< count($users) ; $i++){
if (isset($_POST['username'])&& isset($_POST['password'])) {
// Please check this code
if ($_POST['username'] == $users[$i]['username'] && $_POST['password'] == $users[$i]['password']) {
$data = array('result' => 'success','message' => 'Message sent from server');
echo json_encode($data);
// Please check this code
} else {
// Please check this code
$data = array('result' => 'fail', 'message' => 'Message not sent from server');
echo json_encode($data);
}
}
}
我想從表單的登錄用戶可以檢查用戶(陣列),使用AJAX打印消息,如果匹配用戶與否。
請幫我檢查我的所有代碼並修復它。謝謝大家。
是否有你忘了介紹了一些實際問題呢? 「請檢查我的代碼」並不是一個真正的可回答的問題。 – David