-1

I have a button in login form popup modal <button type="submit" class="btn btn-primary btn-lg btn-block" onclick="checkall();">Login</button>

function checkall()
    {
     var name=document.getElementById( "login_username" ).value;
     var pass=document.getElementById( "login_password" ).value;
      var testing = true;
     if(name)
     {
      $.ajax({
      type: 'post',
      url: 'checkuser.php',
      data: {
       user_name:name,
       user_pass:pass,
      },
      success: function (response) {
       $( '#name_status' ).html(response);
       if(response=="OK") 
       {   
          var getUrl = window.location;
          var baseUrl = getUrl .protocol + "//" + getUrl.host + "/" + getUrl.pathname.split('/')[1];
          var base2 = baseUrl + "/instructor/index.php";
          window.location = base2;          
       }
       else
       {        
        $('#text-login-msg').text("Testing");
          testing = false;
       }
      },
      error: function() {
        $( '#name_status' ).html("");
        return false;
      }

      });      

     return testing; 
     }         
  }

Everything is work I just want to stop the form model to refresh the page and show error on screen if details don't match.

`
 else
       {        
        $('#text-login-msg').text("Testing");
          testing = false;
       }
`

It is supposed to show testing and leave the popup open. But this is not working. I have tried return false also but same problem. I know ajax is async any way around that so that if data don't match then it should stay like that without refreshing page.

Thank you.

Kevin B
  • 94,570
  • 16
  • 163
  • 180
Phoenix
  • 332
  • 2
  • 9
  • 32
  • 1
    Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – David Jan 26 '17 at 02:34
  • Since the success condition results in a redirect, the function might as well *always* return false. Remove the `testing` variable completely, remove all `return` statements you currently have, and just add `return false;` at the end of the function. (But also take a look at the linked question and answers, as it explains why you're experiencing this problem in the first place and will help you understand how asynchronous operations work in JavaScript.) – David Jan 26 '17 at 02:37
  • yes I tried doesn't work. – Phoenix Jan 26 '17 at 02:38
  • Show your attempt and explain what "doesn't work" means. – David Jan 26 '17 at 02:38
  • When I change to return false at the end. It is still refreshing the page. I want my login model to show if data from database doesn't match. – Phoenix Jan 26 '17 at 02:41

1 Answers1

-2

I think your question is: how to keep the popup when response is not "OK" ?

<button type="button" class="btn btn-primary btn-lg btn-block" onclick="checkall(event);">Login</button>

Update the checkall function to prevent Default close action.

function checkall(e){
  e.preventDefault();
  //... rest of your codes;
}

Also be careful with button type submit, it normally refreshes the page.

The type of the button. Possible values are:
submit: The button submits the form data to the server. This is the default if the attribute is not specified, or if the attribute is dynamically changed to an empty or invalid value.
reset: The button resets all the controls to their initial values.
button: The button has no default behavior. It can have client-side scripts associated with the element's events, which are triggered when the events occur.
menu: The button opens a popup menu defined via its designated element.

From the Mozilla Documentation

Hu Qiang
  • 1,862
  • 1
  • 12
  • 7