0

Hello I am not good with ajax.I want to check my login info and return either 'success' or 'fail'.Buy my ajax seems to have an error.

var user = $('.username').value();
var pass = $('.password').value();

$.ajax({
  type : 'POST',
  url  : 'login_check.php',
  data : {
    'username': user,
    'password': pass
  },
  beforeSend: function() {
    $("#Loading").show();
  },
  success :  function(response) {     
    if(response=="success" && response!=="fail") {
      $('.status').html("Success! Now logging in ......");
      setTimeout(' window.location.href = "index.php"; ',4000);
    } else {
      $('#Loading i').hide();
      $('.status').html("Login fail! Please use correct credentials....");
      setTimeout(' window.location.href = "login.php"; ',4000);
    }
  }
});

Can anyone points me out?

Valentin Podkamennyi
  • 7,161
  • 4
  • 29
  • 44
Lin Aung
  • 427
  • 3
  • 15
  • what error you get ? also you dont need if(response=="success" && response!=="fail") remove it – Abslen Char Mar 17 '18 at 18:24
  • Get the error using this and see what it says https://stackoverflow.com/questions/1637019/how-to-get-the-jquery-ajax-error-response-text – Robert Mar 17 '18 at 18:34

3 Answers3

4

The reason you are getting error is because your javascript is getting break(giving error) at $('.username').value(); as there is no value() function. If you open console you get this error. So because of this rest of script is not working. So change $('.username').value(); to this $('.username').val(); and same for the var pass = $('.password').value(); change to var pass = $('.password').val(); and also you don't need if condition as mention in comment. Your final code will be something like this.

var user = $('.username').val();
var pass = $('.password').val();
$.ajax({
    type: 'POST',
    url: //some url
        data: {
            'username': user,
            'password': pass,
        },
    beforeSend: function() {
        //some code
    },
    success: function(response) {
        // some code which you want to excute on success of api
    },
    error: function(xhr, status, error) {
        // some code which you want to excute on failure of api
    }
});
yajiv
  • 2,901
  • 2
  • 15
  • 25
0

I dont have the whole code for your app but when it come to your ajax request your code should look like this , for a more accurate answer please show the error that you are getting

  var user = $('.username').val();
    var pass = $('.password').val();
    $.ajax({
    type : 'POST',
    url  : 'login_check.php',
    data : {
                'username':user,
                'password':pass,
                },
    beforeSend: function()
    { 
     $("#Loading").show();
    },
    success :  function(response)
       {      

       $('.status').html("Success! Now logging in ......");
       setTimeout(()=>{ window.location.href = "index.php"; },4000);
       },
    error: function(xhr, status, error) {

       $('#Loading i').hide();
       $('.status').html("Login fail! Please use correct credentials....");
       setTimeout(()=>{ window.location.href = "login.php"},4000);

      }
    });
Abslen Char
  • 3,071
  • 3
  • 15
  • 29
0

Your response needs to be a PHP echo that returns a string with a value of either ”success” or ”fail”.

Your PHP response after successful login:

echo(‘success’);

Your PHP response after failed login:

echo(‘fail’);

suchislife
  • 4,251
  • 10
  • 47
  • 78