views:

414

answers:

4

Hi, I'm making a basic ajax function in jquery which echoes the number of rows found in a MySQL Query.

function checkEventIDClass(id) {
                var params = 'method=checkEventIDClash&table=test&id=' + id;
                $.ajax({
                type: "POST",
                url: "ajax.php",
                data: params,
                    success: function(result){       
                    return result;
                    }
                }); 
       }

Is it possible to use this returned value in another function? I have tried but only get undefined values. In this situation, it will be acceptable to use synchronous calls.

Any advice appreciated.

Thanks

+1  A: 

Add

async:false

to the options list:

function checkEventIDClass(id) {
                var res;
                var params = 'method=checkEventIDClash&table=test&id=' + id;
                $.ajax({
                type: "POST",
                async:false,
                url: "ajax.php",
                data: params,
                    success: function(result){       
                    res=result;
                    }
                }); 
                //Now in res variable you have the ajax request result
       }

In this way the checkEventIDClass waits until the ajax request finishes and then gets the result. In your code the function doesn't wait and the result is undefined.

mck89
That still won't return a value.
SLaks
You're right i forgot to write some code. Anyway now in the checkEventIDClass context you have the result of the request
mck89
+5  A: 

This is not possible with asynchronous calls, because your checkEventIDClass function returns its value before the callback is executed. When you write return result, you are returning from the callback to the jQuery code that called it in the first place, which ignores your return value.

You should change your checkEventIDClass function to take a callback instead of returning the value, like jQuery.ajax itself. For example:

function checkEventIDClass(id, callback) {
    var params = 'method=checkEventIDClash&table=test&id=' + id;
    $.ajax({
        type: "POST",
        url: "ajax.php",
        data: params,
        success: function(result) { 
            callback(result);
        }
    });
}

Alternatively, you can make a synchronous request. However, I would not recommend that, because a synchronous request will completely freeze the browser until the server responds.

SLaks
+2  A: 

In jQuery manual there are some examples

 var html = $.ajax({
  url: "some.php",
  async: false
 }).responseText;
Vertigo
A: 

Assuming I'm understanding you...

function checkEventIDClass(id) {
    var params = 'method=checkEventIDClash&table=test&id=' + id;
    var my_temp_var = 0;
    $.ajax({
     type: "POST",
     url: "ajax.php",
     data: params,
     async: false,
     success: function(result){
      my_temp_var = parseInt(result, 10);
     }
    });
    return my_temp_var;
}

Note that because you state the result is a number of rows, I'm explicitly parsing it as an integer.

artlung