0

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

2
Contributors
4
Replies
5
Views
4 Years
Discussion Span
Last Post by zoreli
0

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?

0

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

0

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...

Edited by Taywin

0

Hi Taywin

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

Cheers

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.