我有一个相当简单的登录表单,它使用 jQuery AJAX 请求提交。目前,提交它的唯一方法是按下“登录”按钮,但我希望能够在用户按下“Enter”时提交表单。
我只使用 jQuery AJAX 请求完成过表单提交,但我不确定我需要进行哪些修改才能在用户按下“Enter”时提交表单。
HTML:
<form>
<label for="username">Username</label>
<input type="text" id="username" placeholder="Username" />
<label for="password">Password</label>
<input type="text" id="password" placeholder="Password" />
</form>
<button id="login">Login</button>
JavaScript:
$(document).ready(function() {
$('#login').click(function() {
$.ajax({
type: "POST",
url: 'admin/login.php',
data: {
username: $("#username").val(),
password: $("#password").val()
},
success: function(data)
{
if (data === 'Correct') {
window.location.replace('admin/admin.php');
}
else {
alert(data);
}
}
});
});
});
摘自 login.php:
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = :username AND password = :password");
$stmt->execute(array(
':username' => $user,
':password' => $pass
));
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
$affected_rows = $stmt->rowCount();
if ($affected_rows == 1) {
//add the user to our session variables
$_SESSION['username'] = $username;
echo ('Correct');
} else {
echo 'Incorrect username/password';
}