php – jQuery / AJAX登录表单提交输入

我有一个相当简单的登录表单,使用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';
}
在表单中添加ID并将登录按钮转换为提交按钮:
<form id="myform">

<label for="username">Username</label>
<input type="text" id="username" placeholder="Username" />

<label for="password">Password</label>
<input type="text" id="password" placeholder="Password" />

<input type="submit" id="login" value="Login"/>

</form>

然后,而不是使用click事件:

$('#login').click(function() {

使用提交事件:

$('#myform').submit(function() {

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...