输入时提交jQuery / AJAX登录表单

时间:2012-12-05 01:48:19

标签: php javascript 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';
}

3 个答案:

答案 0 :(得分:8)

在表单中添加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>

然后,而不是使用点击事件:

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

使用提交事件:

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

答案 1 :(得分:0)

​$('form').on('keyup', function(e){
    if(e.which == 13 || e.keyCode == 13){
        alert('enter pressed');
    }        
});​​

答案 2 :(得分:0)

<强> HTML

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

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

    <button id="login">Login</button> 
</form>

<强> JavaScript的:

$(document).ready(function() {

    $('#myform').submit(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);
                }
            }
        });
        //this is mandatory other wise your from will be submitted.
        return false; 
    });

});