JQuery login error message works only once

Viewed 53

I have a login form which display an error message when there is bad ID or password. It works but if user enter more than once a bad ID or login, the message is printed only once. How to fix it ? This my JS script :

<script type="text/javascript">
$(document).ready(function() {
    $("#buttonConnection").click(function(e) {
        e.preventDefault();
        $.post(
            'mypost.php',
            {
                myID: $("#myID").val(),
                myPassword: $("#myPassword").val()
            },
            function(data) {
                if (data == 'Success') {
                    document.location.href = "dashboard.php"
                } else {
                    $("#resultat").html('Bad ID or password !');
                    $("#resultat").addClass("alert alert-danger").fadeTo(3000, 0);
                }
            },
            'text'
        );
    });
});
</script>
1 Answers

Once the fade is complete, have a callback which resets the resultat HTML and classes:


<script type="text/javascript">
$(document).ready(function() {
    $("#buttonConnection").click(function(e) {
        e.preventDefault();
        $.post(
            'mypost.php',
            {
                myID: $("#myID").val(),
                myPassword: $("#myPassword").val()
            },
            function(data) {
                if (data == 'Success') {
                    document.location.href = "dashboard.php"
                } else {
                    $("#resultat").html('ID or password incorrect !').addClass("alert alert-danger").fadeTo(3000, 0, function() {
                        $(this).html('').removeClass("alert alert-danger").fadeTo(0, 1);
                    });
                }
            },
            'text'
        );
    });
});
</script>

Related