4

I have a fairly simple login form that is submitted with a jQuery AJAX request. Currently, the only way to submit it is to press the "Login" button, but I would like to be able to submit the form when the user presses "Enter".

I have only ever done form submissions using jQuery AJAX requests, but I'm unsure of what modifications I would need to make in order to have the form also be submitted when the user presses "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);
                }
            }
        });

    });

});

Excerpt from 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';
}
Jordan
  • 859
  • 4
  • 16
  • 36

3 Answers3

8

Add an ID to your form and transform your login button into a submit button :

<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>

Then, instead of using the click event:

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

use the submit event:

$('#myform').submit(function() {
tmuguet
  • 1,165
  • 6
  • 10
  • 2
    don't forgot to return false inside the handler function because you don't want to really submit the form. – MatRt Dec 05 '12 at 02:02
1

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; 
    });

});
Juned Ansari
  • 4,684
  • 4
  • 49
  • 82
0
​$('form').on('keyup', function(e){
    if(e.which == 13 || e.keyCode == 13){
        alert('enter pressed');
    }        
});​​
Ohgodwhy
  • 47,967
  • 9
  • 74
  • 103
  • This, except give your anonymous click function a name (`$('#login').click(doFormSubmit);`) then replace `alert('enter pressed');` with `doFormSubmit();` – joequincy Dec 05 '12 at 02:00