2

jQuery function return true or false, returning only false while it's all good

I don't know how to fix it and what wrong.

So far i got to this , the code :

the onclick function:

$(document).on('click', '.founditems', function (e) {
    var $this = $(this)
    var k = $this.attr('href');
    if (checkUserSystem(k) == true) {
        alert("good");

    } else {
        alert("bad");
    }
});

the ajax function:

function checkUserSystem(k) {
    var starttime = $.trim($('#checkusersystemstartime').val());
    var k = k;
    var dataString = 'k=' + k + '&starttime=' + starttime;
    $.ajax({
        type: "POST",
        url: "r.php?proc=sendCp",
        data: dataString,
        dataType: 'json',
        beforeSend: function () {

        },
        complete: function () {

        },
        success: function (data) {

            if (data.message == false) {

                return false;
            } else if (data.cblocked == true) {

                return false;
            } else {

                return true;
            }
        }
    });
    return false;
}

The function always return false when it's working perfect and the PHP return true, And if I remove return false; at the end of the ajax code, it will return undefined instead of true any idea?

Thanks.

Nmk
  • 1,281
  • 2
  • 14
  • 25
Raviv g
  • 55
  • 1
  • 1
  • 8

3 Answers3

4

You will have to read up on async execution. The success block is run when the ajax call is 'complete, but the execution of your code will continue right away after the $.ajax() call.

The reason for the undefined error is that the method doesn't return anything unless you ad the 'return false' call.

Either you can take the alert true/false part as a function parameter or you will have to look at the synchronous version of ajax calls.

Luc
  • 273
  • 1
  • 9
  • ok thanks any more info about it ? – Raviv g Nov 29 '13 at 10:04
  • Here's some refs. Sorry, but others have described this to large extents already. http://stackoverflow.com/questions/13286233/javascript-pass-function-as-parameter http://stackoverflow.com/questions/133310/how-can-i-get-jquery-to-perform-a-synchronous-rather-than-asynchronous-ajax-re http://api.jquery.com/jQuery.ajax/ – Luc Nov 29 '13 at 10:07
  • And to be clear, I would really recommend looking at solutions to continue the use of the async version (that is for ex. passing a method to be executed on success). The async execution is something good (doesn't block while you wait for the server), so going for the sync version should really have a better reason that you'd have to think something over :) – Luc Nov 29 '13 at 10:14
1

You can also use the success part like this

function (data) {
    if (data.message) {
        return true;
    } else if (data.cblocked) {
        return false;
    } else {
        return false;
    }
}
Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
1

You can't use " if(checkUserSystem(k)==true)" because your ajax request is asynchronous request. It mean checkUserSystem function always will return "false" before your ajax request is completed.

I think if you have to do something after ajax request is complete you should do it in ajax.success function:

$(document).on('click','.founditems',function(e){

                       var $this=$(this)

                       var k = $this.attr('href');

                       checkUserSystem(k);

 });

function checkUserSystem(k){

    var starttime = $.trim($('#checkusersystemstartime').val());

    var k = k;

    var dataString = 'k='+ k + '&starttime=' + starttime;

    $.ajax({
    type: "POST",
    url: "r.php?proc=sendCp",
    data: dataString, 
    dataType:'json',
    beforeSend: function(){

      },    
    complete:function(){

      },
    success: 
           function(data){

            if(data.message == false){

                  alert("good");

             }else if(data.cblocked == true){

                   alert("good");

                 }else{

                   alert("good");
             }


    }



    });
return false;
} 
Alex Nguyen
  • 1,032
  • 12
  • 27