简体   繁体   中英

Hide Loading Data, Once Page has loaded

Been asking a lot of jquery questions lately, I'm trying my best to learn a lot about it.

Anyway, I am sending an Ajax HTTP request to a PHP page in order to make a login form without requiring a page refresh. Now since it may take some time to connect the database and get the login information etc..

Now I do have loading as a .html but how can I hide the loading data once data has loaded?

Tried to use a few functions but didn't seem to work.

Thanks so far, this site has helped me a lot through the learning process.

Here's the JavaScript:

$(document).ready(function() {
    // Make a function that returns the data, then call it whenever you
    // need the current values
    function getData() {
        return {
            user_login: $('#user_login').val(),
            pass_login: $('#pass_login').val()

        }
    }


        function loading(e) {
        $('#content').html('Loading Data');
    }

    function check(e) {
        e.preventDefault();
        $.ajax({

            url: 'ajax/check.php',
            type: 'post',
            data: getData(), // get current values
            success: function (data) {
                            $('#content').hide('slow');

                alert(9);

            }

        });

    }

    // Don't repeat so much; use the same function for both handlers
    $('#field').keyup(function(e) {
        if (e.keyCode == 13) {
    var username = $('#user_login').val();

        loading(e);
        check(e);

        }
    });

    $('#submit').click(function(e) {
        if (e.keyCode != 13) {
        loading(e);
        check(e);
        } 


    });

});

Here is the HTML:

    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
                <script src="js/login.js"></script>
                <div id="content"> Loading...</div>

                <div id="field">
            <input type='text' name='user_login' id='user_login' placeholder='eg: Mark@gmail.com'> <br>
            <input type='password' name='pass_login' id='pass_login'> <br>
        <input type="submit" name="submit" id="submit" value="Login">
            </div>

First of all, i'm not sure if your code will return a success. This code will help you find out if the response is not successfully. If you get a browser alert. It means you have a server side error. Its either the url path is wrong or your server response statuscode is 500 (Internal error).

$.ajax({

        url: 'ajax/check.php',
        type: 'post',
        error: function(){
           alert('An error has occured.');
        },
        beforeSend: function(){
            $('#content').show('slow').html('Loading');
            //loading(e);
        },
        data: getData(), // get current values
        success: function (data) {
                        $('#content').hide('slow');

            alert(9);

        }

});

Refactoring your code entirely will look like this. Choose first or second:

$.ajax({

        url: 'ajax/check.php',
        type: 'post',
        error: function(){
           alert('An error has occured.');
        },
        beforeSend: function(){
            $('#content').show('slow').html('Loading');
            //loading(e);
        },
        data: {user_login: $('#user_login').val(),pass_login: $('#pass_login').val()}, // get current values
        success: function (data) {
                        $('#content').hide('slow');

            alert(9);

        }

});
function check(){
    $.ajax({

        url: 'ajax/check.php',
        type: 'post',
        error: function(){
           alert('An error has occured.');
        },
        beforeSend: function(){
            $('#content').show('slow').html('Loading');
            //loading(e);
        },
        data: {user_login: $('#user_login').val(),pass_login: $('#pass_login').val()}, // get current values
        success: function (data) {
                        $('#content').hide('slow');

            alert(9);

        }

   });
}

$('#submit').click(function(){
   check();
});

$('#field, #field input').keyup(function(e){
        var username = $('#user_login').val();
        if (e.keyCode == 13) {
        check();
        } 
});

Markup

    <div id="field">
        <input type='text' name='user_login' id='user_login' placeholder='eg:Mark@gmail.com'> <br>
        <input type='password' name='pass_login' id='pass_login'> <br>
        <input type="button" name="submit" id="submit" value="Login">
    </div>

Good luck!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM