JNK
JNK

Reputation: 1753

javascript: returning a bool

I have written a jQuery / JS function which "runs a php query".

function runQuery(op){
if(op.type == "edit"){
    var b = false;
    if(op.id != "" && (op.fromSong || op.toSong || op.when || op.comment)){
    $.post("processor.php", { id: op.id, type: "edit", fromSong: op.fromSong, toSong: op.toSong, when: op.when, comment: op.comment }, function(data){
        if(data == true){
            console.log(true);
            b = true;
        }else{
            console.log(false);
             b = false;
        }
    });
    }
    return b;
}

I want it to return true of false depending on what the server answers. I'm sure that the php script is working correctly and returning true or false correctly. But every time i run the function the console.log() outputs the correct value, unlike the variable b. It seems to alway be false. What am I doing wrong?

Upvotes: 0

Views: 113

Answers (4)

Silver Light
Silver Light

Reputation: 45922

This is a wrong way to do it. Ajax requests are asynchronous and not performed one after another as you expect them too. There is no way around that. You will have to redesign your code to nest ajax calls.

Something similar to this:

$.post("a.php", {function(data){
    //1st check 
    $.post("b.php", {function(data){
        //2nd check 
        $.post("c.php", {function(data){
            //final actions
        });  
    });    
});

Only this way ajax calls will be stacked - the next will be performed after the previous.

Upvotes: 0

user229044
user229044

Reputation: 239301

Your function will return before the asynchronous request to the server has completed. Before you ask, it is both impractical and undesierable to make the call synchronous so that you can return true/false from your function. You might consider supplying a callback argument or arguments to your function:

function runQuery(op, success, failure){
   // ...
        if(data == true){
          success();
        }else{
          failure();
        }
   // ...
}

runQuery('edit',
  function () { alert('success!'); },
  function () { alert('failure!'); }
);

Upvotes: 0

Baptiste Pernet
Baptiste Pernet

Reputation: 3384

yes it is wrong.

$.post is done asynchronously, so b is set in the call back but returned before you reach the callback.

you should use $.ajax instead with method:"POST", and async: false

However it could be better to have a deisgn when you use the value of b in the callback only because async: false can freeze your browser.

Upvotes: 0

jAndy
jAndy

Reputation: 236022

Since any .ajax() call ($.post is just a wrapper to $.ajax) runs asyncronously, your variable b will always return false. Best thing to "workaround" this is to pass in another callback:

function runQuery(op, callback){
    if(op.type == "edit"){
        if(op.id != "" && (op.fromSong || op.toSong || op.when || op.comment)){
            $.post("processor.php", { id: op.id, type: "edit", fromSong: op.fromSong, toSong: op.toSong, when: op.when, comment: op.comment }, function(data){
        if(data == true){
            console.log(true);
            callback.apply(this, [data]);
        }else{
            console.log(false);
            callback.apply(this, [data]);
        }
     });
    }
}

runQuery({
    type: 'edit',
}, function(data) {
   alert(data);
});

Upvotes: 3

Related Questions