简体   繁体   中英

seems that login-page is reloading after false form-validation

I was changing my loginpage and I've put the jquery with the form-validation in a separate file instead of on the page itself.

$(document).ready(function(){
    $("#confirm").on("click", function(e){
       e.PreventDefault();

        var check = true;
        var empty_fields = ['name','password'];

        for (i=0; i<empty_fields.length; i++) {
            var $field = $('#'+empty_fields[i]);

            if ($.trim($field.val()) == '') {
                $field.addClass('error');
            check = false;
            } 
            else {
                $field.removeClass('error');
            }            
        }

        if (check == true)
        {
            $.ajax({
                type: 'POST',              
                url: 'PHPCalls.php?CallID=Login',
                data: $("#loginform").serialize(),
                success: function(data) {                    
                    var result = $.trim(data);
                    if(result == 'true') {
                        alert('succeeded');
                    }
                }
            });
        }
    });
});

Before the part where I do my check on the input being empty was on the login-page itself and worked fine. I called it with an onclick="return check_form();" and then returned the value 'check' being true or false.

Since I've put this script in a separate file it seems like the page is reloading itself. When I click on the confirm-button the input-boxes get the error-layout but then the page flashes and all is set back to normal...I have no clue what is happening...

Anyone can set me on the right track?

This is a stripped part of the login-page

<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script type="text/javascript" src="js/ajaxLogin.js"></script>
    <script type="text/javascript" src="js/sha512.js"></script>
    <script type="text/javascript" src="js/forms.js"></script>
</head>
<body>
<form name="loginform" class="loginform" action="#" method="post">
    <input type="text" name="name" id="name" class="input username"  placeholder="Name" onfocus="this.value=''" />
    <input type="password" name="password" id="password" class="input password"  placeholder="Wachtwoord" onfocus="this.value=''" />
    <input type="submit" id="confirm" value="Login" class="button" />
</form>
</body>

SOLUTION: add PreventDefault() and remove formhash()

You need to return false if check! = true

if (check == true)
    {
        $.ajax({
            type: 'POST',              
            url: 'PHPCalls.php?CallID=Login',
            data: $("#loginform").serialize(),
            success: function(data) {                    
                var result = $.trim(data);
                if(result == 'true') {
                    alert('succeeded');
                }
            }
        });
    }
else
{
    return false;
}

Try this

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