JavaScript验证后PHP函数无效[关闭]

问题描述:

I used validation for my login field with jQuery & Javascript, and it works. When there is no username or password the span text changes.

When the details are correct and I press login it just reloads the page again, instead of starting the session etc.

But if I enter a valid username with a wrong password, PHP kicks in and displays incorrect password or if I enter an invalid username, it says username not found.

This is such a weird problem. I have a feeling it's due to the fact I am returning a flag to check sending, after the Javascript validation:

$(document).ready(function() { 
    $('#loginf').submit(function(){ 
        var flag = true

        //stuff to validate each field 
        //ie: for each field i have:

       if(//empty field) { 
        //stuff flag= false; 
       } 
       return flag; 
   }); 

});

If you don't want your page to reload, you should always have

return false;

in the end of your validation/submit code in JS.

Otherwise, it will always the default action for the button, in this case, follow the action of the form.

Are you starting the session in PHP? If you are doing this by AJAX, try checking what data parameters are you sending and receiving with Firebug.

You should show you php code here Btw , you should write return false; in validation script because that code will not work when page will reload.