0

js:

 function verificaExistPed(numped){
        var valida;
        jQuery.post("procedures/class_oc.php", // ajax post
            {
                cache      : false,
                checkYear  : true,
                numped     : numped
            },
            function(data)
            {
                if(data === "s"){
                    valida = true;
                }else{
                    valida = false;
                }
            }
        )
        return valida;
  }

and, calling the function in another place, should return valida result inside variable check, in my case, true or false.

var check = verificaExistPed('".$numped."');
alert(check); // always undifined

but, always undefined.

how can i set valida to true or false from a $.post callback ?

Ricardo Binns
  • 3,228
  • 6
  • 44
  • 71
  • possible duplicate of [JavaScript asynchronous return value / assignment with jQuery](http://stackoverflow.com/questions/7779697/javascript-asynchronous-return-value-assignment-with-jquery) – Felix Kling Dec 07 '11 at 11:15

2 Answers2

2

It's because handler are invoked asinchronously after your functon invoked. So you synchronously request for it, like:

function test() {
    var html = $.ajax({
    url: "procedures/class_oc.php",
       async: false // <-- heres the key !
    }).responseText;

    return html;
}
Samich
  • 29,157
  • 6
  • 68
  • 77
  • Making synchronous calls is a bad idea in general, as it can freeze the browser. – Felix Kling Dec 07 '11 at 11:16
  • Yep, but in this case if OP need a result of the function with ajax request it's only way to do it. – Samich Dec 07 '11 at 11:17
  • 1
    No it's not the only way. He should pass a callback instead and put all the logic that needs this result into the callback. It's pretty straightforward with jQuery's deferred objects (see the duplicate question I linked to). – Felix Kling Dec 07 '11 at 11:18
  • This will freeze the browser (atleast IE) – Abdul Munim Dec 07 '11 at 11:18
  • If you passing the callback it's already not a result of invoking, it will invoke your callback after ajax call and it can be not a case in OP situation if he needs this value just after calling. – Samich Dec 07 '11 at 11:20
  • That's how you are going to design to work along async calls. Hampering UX is the worst thing ever. – Abdul Munim Dec 07 '11 at 11:34
1

You cannot return from as jQuery.post is an asynchronous call. You have to rely on a callback function in order to get the response from server. Try this:

 function verificaExistPed(numped, isValidCallback){
        jQuery.post("procedures/class_oc.php", // ajax post
            {
                cache      : false,
                checkYear  : true,
                numped     : numped
            },
            function(data)
            {
                isValidCallback(data === "s");
            }
        )
  }

USAGE:

verificaExistPed('".$numped."', function(isValid) {
   alert(isValid); //returns true or false
});
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
Abdul Munim
  • 18,869
  • 8
  • 52
  • 61