4

I have a function to validate a code with AJAX(PHP). However, when I try to run it, it returns undefined when it should return true or false.

function:

function verifyViite(viite) {
    $.get('dataminer.php?question=validateViite&q='+viite, function(data) {
        jQuery.globalEval(data);
        if(valid == 1) {
            return true;
        }
        else {
            return false;
        }
    });
}

The data is either valid = 1; or valid = 0;

Axel Latvala
  • 576
  • 3
  • 7
  • 21

2 Answers2

5

You can't return the Callback return value due to the AJAX request being asynchronous.

Execute whatever code calling verifyViite inside the callback itself, instead of returning true or false.

Alternativly, you can have synchronous AJAX request.

Community
  • 1
  • 1
Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
  • I'm sorry if I understood wrong, but are you saying that I cannot do this validation through a function? – Axel Latvala Oct 09 '11 at 12:13
  • You can do this validation through a function, but you rather need to provide a callback(like I posted in my other post..) – japrescott Oct 09 '11 at 12:15
  • You can't do it in the way you're trying now - you'll have to change your logic. Post more code and we might be able to give you better logic. – Shadow The GPT Wizard Oct 09 '11 at 12:16
  • one question; Why would one ever want to perform Asynchronous requests? – Axel Latvala Oct 09 '11 at 13:20
  • Synchronous request means the browser is waiting until the request is done.. if the server takes long time to load it cause the whole page to "hang" until it finish - not very good experience for the user. By having it asynchronous the page will load as usual and when the data is ready, you can update the proper part in the page. – Shadow The GPT Wizard Oct 09 '11 at 15:15
2

since the function veryViite doesn't return anything, undefined is the expected result. Since its asynchronouse, you would have to give a callback function that would be called on successful ajax call. Something like this;

function verifyViite(viite,cb) {
    $.get('dataminer.php?question=validateViite&q='+viite, function(data) {
        jQuery.globalEval(data);
        if(valid == 1) {
            cb(true);
        }
        else {
            cb(false);
        }
    });
}

and the call would be like;

verifyViite(viite,function(good){
    alert(good);
});
japrescott
  • 4,736
  • 3
  • 25
  • 37
  • Ok so `cb` is a function here? – Axel Latvala Oct 09 '11 at 12:21
  • Could you explain to me this "callback" thing? – Axel Latvala Oct 09 '11 at 12:27
  • Yes. As you see from how I'm calling verifyViite, I'm providing a function which will be called when the ajax returns. So, the function provided is the variable cb within verifyViite. That function would make more sense if it would count errors and maybe flag a document as "not loadable". At the end of your calls you can then figure out what to do with the errorous calls.. Btw -> using Eval is "bad" practice! JQuery will do all the "eval" neccessary for you to access the information. – japrescott Oct 09 '11 at 12:28
  • ok, callbacks -> normally, when you call an asynchronouse function (a function, that can't provide a direct result, cause its doing something in the background and you need to wait), you jump out from the logic of how your code does something. If we provide a function to the asynchronouse function, we can make sure, that function will be called when the asnychronous function returns back. Therefor, it is called "a callback function". helped? – japrescott Oct 09 '11 at 12:32
  • Oh, so that `function (good)` will be called after the server has responded? – Axel Latvala Oct 09 '11 at 12:35
  • And it will contain true/false? – Axel Latvala Oct 09 '11 at 12:35
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/4117/discussion-between-akke-and-japrescott) – Axel Latvala Oct 09 '11 at 12:41