简体   繁体   中英

Notifications works only First time - AJAX

I have created a login system. Login Form submission through AJAX.

However the Response show only the first time. ie. Suppose i enter the Wrong credentials first. The response shows. But when I enter new credentials again it doesn't show the response message. Sorry I'm relatively new to JQuery.

Gven below is the Jquery code

<script type="text/javascript">
    $(document).ready(function() {
        var form = $('#loginform');
        form.submit(function (ev) {
        ev.preventDefault();
        $.ajax({
            type: form.attr('method'),
            url: form.attr('action'),
            cache: false,
            data: form.serialize(),
            success: function (data) {
                if(data == 1){
                    $("#user-result").html("<font color ='#006600'> Logged in | Redirecting..</font>");
                    setTimeout(
                    function() 
                    {
                        window.location.replace("index.php");
                    }, 1000);
                }
                else{
                    $("#user-result").html(data).delay( 2500 ).fadeOut( 800 );
                }
           }
        });        
    });
});
</script>

I think it might be the problem with Fadeout. Any Help?

Since you are using fadeOut(), the user-result element is hidden when the second notification comes. So

$("#user-result").html("<font color ='#006600'> Logged in | Redirecting..</font>").show();
$("#user-result").html(data).show().delay( 2500 ).fadeOut( 800 );

Yes, the problem with fadeout that hides message container. Before showing new message you have to show container back:

$("#user-result").html("<font color ='#006600'> Logged in | Redirecting..</font>").show();

once any element fadeout, it wont appear until page reloaded or fadeIn()

                    if(data == 1){
                        $("#user-result").html("<font color ='#006600'> Logged in | Redirecting..</font>").fadeIn("slow");

                        setTimeout(
                              function() 
                              {
                                window.location.replace("index.php");
                              }, 1000);

                    }
                    else
                    {
                        $("#user-result").html(data).delay( 2500 ).fadeOut( 800 );
                    }

when data ==1, use fadeIn()

You need to use setInterval(function(){}); in order to cycle each time.

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