0

This should really be simple. But the code I'm trying to write is just not working, even after searing around the internet.

So I have this. code to check if a user is online or offline.

//Check if it's a valid user
function checkUser(){
status = (function(){
    var isOnline = null;
    $.ajax({
        async: false,
        global: false,
        type: "POST",
        url: "./system/checkUser.php",
        data: $("#webcam").serialize(),
        success: function(data){
            isOnline = (data == "1");
        }
    });
    return isOnline;
})();

return status;
}

and this is how I try to use it

function loadProfile(){
var text = checkUser();
alert(text);

if(text != true){
    alert("Invalid user!");
    return;
}else{
    //Code
}

But whatever I get from checkUser(), I always get the "Invalid user!" alert, even when text is true. Can anyone help me here? I'm lost for words.

Yemto
  • 613
  • 1
  • 7
  • 18
  • `async: false` is deprecated, and usually doesn't work any more. – jcarpenter2 Feb 08 '14 at 03:53
  • @user2245885 can you post what is reeturned from your php ? – Amit Joki Feb 08 '14 at 03:53
  • What gets alerted? And what does your script return? – godfrzero Feb 08 '14 at 03:54
  • 2
    Please have a look at [How to return the response from an AJAX call?](http://stackoverflow.com/q/14220321/218196) to learn how to avoid synchronous calls. – Felix Kling Feb 08 '14 at 03:55
  • The php script is a simple `prepare("SELECT COUNT(*) FROM profile WHERE id=:id AND password=:pass");` and returns a 0 or a 1 depending on if the details are right. I get alerted "true", and then "Invalid user!". But I can also get alerted "false" and "Invalid user!" which shouldn't be possible – Yemto Feb 08 '14 at 03:59
  • Two things you could try: Make `status` local. Currently you are assigning to the **global** variable `status`, which is already a predefined global variable. You should do that in any case. *"even when text is true"* Note that the **string** `"true"` is not equal to the **boolean** `true`. – Felix Kling Feb 08 '14 at 04:02
  • Also worth a read, if you want to learn more about promises in jQuery: http://learn.jquery.com/code-organization/deferreds/. – Felix Kling Feb 08 '14 at 04:07
  • JavaScript doesn't "ignore" anything here. Always *suspect your own code first* before making such claims. – user2864740 Feb 08 '14 at 04:10

2 Answers2

3

As of jQuery 1.8, the use of async: false with jqXHR ($.Deferred) is deprecated; you must use the success/error/complete callback options instead of the corresponding methods of the jqXHR object such as jqXHR.done() or the deprecated jqXHR.success().

http://api.jquery.com/jquery.ajax/


What you can do is:

function checkUser(callback){
    $.ajax({
        global: false,
        type: "POST",
        url: "./system/checkUser.php",
        data: $("#webcam").serialize(),
        success: callback
    })
}

function loadProfile(){
    checkUser(function(text){
        alert(text);

        if(text != true){
            alert("Invalid user!");
            return;
        }else{
            //Code
        }
    });
}
Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
1

async: false is deprecated so you have to use a callback to access data from an ajax request.

You could change your checkUser function so that it takes a success callback as a parameter.

I personally don't like using big callbacks, so I usually get around the problem with a $.Deferred:

function checkUser() {

    status = $.Deferred();

    $.ajax({
        global: false,
        type: "POST",
        url: "./system/checkUser.php",
        data: $("#webcam").serialize(),
        success: function(data) {
            status.resolve(data == "1");
        }
    });

    return status;
}

function loadProfile() {

    var textD = checkUser();

    textD.done(function (text) {
        alert(text);

        if(text != true){
            alert("Invalid user!");
        }
    });
}
jcarpenter2
  • 5,312
  • 4
  • 22
  • 49