Ajax在登录表单中显示错误,而不重新加载页面


Ajax displaying errors in login form without reloading page

我有一个登录表单,当出现错误时,即"电子邮件地址未注册",该表单当前会重新加载到新页面。我需要在重新加载到新页面的同时将错误显示在同一页面上。我遵循了一个关于如何使用Ajax的教程,但它似乎不起作用,页面仍然会重新加载。ajax代码是:

    $("#login_button").click(function(){
    $.post($("#login_form").attr("action"), $("#login_form :input").serializeArray(), function(info){$("#login_errors").html(info);});
    clearInput();
});
$("login_form").submit(function(){
    return false;
});
function clearInput(){
    $("#login_form: input").each(function() {
        $(this).val('');
    }); 
}

我的登录表单代码是:

<form id= "login_form" action="login.php" method="post">
    <span id="login_errors"></span>
    <label>Email Address</label>
    <input type="text" name="email" id="email" required="required"/>
    <br />
    <label>Password</label>
    <input type="password" name="password" id="password" required="required"/>
    <br />
    <div class="checkbox">
    <input id="remember" type="checkbox" />
    <label for="remember">Keep me signed in</label>
    </div>
    <div class="action_btns">
    <div class="one_half last"><input type="submit" class="btn btn-blue" id="login_button" value="Login"></div>
    <div class="one_half last"><a href="#" id="register_form" 
class="btn">Sign up</a></div>
        </div>
</form>

我将ajax脚本链接到:

<script type="text/javascript" src="login_Ajax.js"></script>

您应该防止提交按钮触发默认操作(提交)。

像这样更新你的代码:

   $("#login_button").click(function(){
    $.post($("#login_form").attr("action"), $("#login_form :input").serializeArray(), function(info){$("#login_errors").html(info);});
    clearInput(); 
    // Prevent the default action from occurring.
    return false;
});

当您开始使用AJAX时,您必须做的第一件事是确切地知道如何通过类等元素将信息显示到div或HTML5的任何标记中。请看下一个代码,因为我会为您评论它。

//This is our DOM on JQUERY or JAVASCRIPT named as main.js
$(function() {
    //First we have to listen on a event click (button) and extract the values of the inputs
    $('body').on('click','.myButtonClass', function() {
        $email    = $('#emailID').val();
        $password = $('#passwordID').val();
        //We save in a object our values
        $x = {
            action:'register',
            email:$email,
            password:$password
        };
        //We execute ajax calling a php file for POST method
        $.post('file.php',$x,function(callback) {
            $('.answer').html(callback);
        });
    });
});
/*END OF DOM AND MAIN.JS*/
//BEGIN OUR PHP FILE WHERE WE'LL RECIEVE ALL THE DATA FROM THE AJAX FILE
<?php
    //we save the action sended trough ajax
    $action = $_POST['action'];
    //we do a switch for determinate what value have the action variable
    switch ( $action ) :
        case 'register': 
            //we save with addslashes for security in the data base
            $email = addslashes($_POST['email']);
            $password = addslashes($_POST['password']);
            //If your objetive is validate if the email and password were send in the form, you can do something like this
            $validate = strlen($email)*strlen($password);
            //This say that if email * password are > 0, the data were send. Otherwise, this operation gonna be equal zero 
            //and thats means that both or one of the data were not recieve in this file trough ajax.
                if ( $validate > 0 ) :
                    echo 'You login!';
                else :
                    echo 'You are not write all the inputs.';
                endif;
        break;
    endswitch;
?>
/*END OF PHP FILE*/
//BEGIN OUR FORM IN HTML
<form>
    <input type="email" id="emailID" placeholder="Write here your email">
    <input type="password" id="passwordID" placeholder="Write here your password">
    <input type="button" class="myButtonClass">
</form>
<!--This will be where we display the answer with ajax, in the element who have the class answer-->
<div class="answerswer"></div>