Hi

After learning jquery for about a month, I make an attempt to make username & password authentication form. Howver, i get stuck 2 days now, and i will soon start bumping my head against the wall.

What I am trying to do is if user enter the wrong username or password to display the message without reloading the page, and if he enter right username and password, php page that make the check against the database should redirect the user.

I will post my function(s) that doesnt work here, and if anyone can help me to make them work it will be great. If not, any link to some step by step tutorial will also do the trick (under the condition that jquery library is used)

Here is what i have now, and it work partially because if the user enter wrong username and password messge is shown to him, but if he enter right username and password, he remain stuck on the login page, due the e.preventDefault()

function 1:

 formLogin.on('submit', function(e){

          $.ajax({
               type: 'POST',
               url: 'loginCheck',
               data: $(formLogin).serialize(),
               dataType: 'json',
               success: function(errorMessage){ 
                   if(errorMessage.length > 0) {  
                       $('#errorMessageTop').fadeIn();  
                   } 
               }
           });

           e.preventDefault();

       });

function 2:

 formLogin.on('submit', function(e){
       $.post('loginCheck', 
       formLogin.serialize(), function(errorMessage) {
           if(errorMessage.length > 0) {  
                       $('#errorMessageTop').fadeIn();  
                   } else if (errorMessage.length == 0) {
                       console.log(errorMessage.length);
                   } 
       }); 
      return e.preventDefault();
       });

Regards, Zoreli

What you did only to prevent if there is an error message. If there is no error message (length==0), what would you do next? Redirect or display something else? In other words, what are statements for else condition?

Hi Taywin

What I am trying to do is to show the error message if the username or password are wrong. If not, I would like the php file that check the user against the database to redirect the user if that is possible. There I have set the session as well for that case, it is just that at this time if user enter correct user name and passwrod, he remain stucked on login page.

Any code snipped about how this should be done properly will be deeply appreciated. Even sudo help will do...

Regards

zoreli,

I am sorry that I can't pin point what to do next because I am not using JQuery (not familiar enough to be an expert) and I have not touched PHP for a long long time (>6 years). Anyway, what I am seeing is that in function 1, you do not do anything after you get the validation back and there is no error. That's the reason why those who enter username & password correctly would be stuck on the page.

If every page has session and authorization set up, you could simply redirect the page if there is no error; otherwise, you should not use Ajax to verify but directly get the server checks for the authentication & redirect to a proper page. Just my two cents...

Hi Taywin

Thanks for your response. Hopefully some other member of this community will be able to help.

Cheers

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.