-1
$.post("../js.php", {state: state},
                            function(data) {
                                return data;
                        });

Here's my jquery code. Like you can see it sends a post request to js.php.

Here's the code for js.php.

{...}
$query = "SELECT * FROM table WHERE x='" . $y . "'";

$result = $mysqli->query($query);

$num_rows = $result->num_rows;

echo $num_rows ? $num_rows : 0;

Now, when I alert the "data" back in the js file then it displays fine. But when trying to return it or assign it to a variable it does not work. The console tells me that the variable that has been assigned the value of data is undefined.

Any thoughts?

Updated code:

var data = null;

$.post("../js.php", {state: state},
function(response) {
data = response;
});

console.log(data);

Still not working. :(

Linus Juhlin
  • 1,175
  • 10
  • 31
  • 2
    `return data;` does nothing within your success callback. You have to do something with the output from the background script. – Matt Aug 06 '12 at 14:01
  • You have to understand AJAX is *asynchronous*, that's why your updated code won't work either. **The response data is only available from inside the `complete` callback**. – bfavaretto Aug 06 '12 at 14:09

5 Answers5

2

the function in the post is a callback function, you cannot return anything from it since there is no one to return it to.

You need to use the data retuened inside the callback, for example:

$.post("../js.php", {state: state},
                            function(data) {
                                $('.someClass').html(data);
                        });
Tomer
  • 17,787
  • 15
  • 78
  • 137
1

The callback you pass to asynchronous methods such as $.post are executed some time in the future, once the async call has returned. The JavaScript execution has moved on and is now somewhere else, so there is nowhere for your callback to return to.

Imagine it like this:

//Some code... executed as you would expect
var data; //Currently undefined

$.post("../js.php", {state: state}, function(response) {
    //Callback is executed later, once server responds

    data = response; //No good, since we already executed the following code
    return response; //Return to where? We have already executed the following code
});

/* More code... we carry on to this point straight away. We don't wait for
   the callback to be executed. That happens asynchronously some time in
   the future */

console.log(data); //Still undefined, callback hasn't been executed yet

If you need to work with the data returned by the async call, do so in the callback.

James Allardice
  • 164,175
  • 21
  • 332
  • 312
0
var data = null;
$(...).click(function(e) {
    $.post("../js.php", {state: state},
                        function(response) {
                            data = response;
                    });
});

After that just access data variable. Keep in mind that its value will be null unless the post request is done.

aemdy
  • 3,702
  • 6
  • 34
  • 49
  • Why did you add a click function to my post function? Also, added my own version of what you wrote. And it did not work. :/ var data = null; $.post("../js.php", {state: state}, function(response) { data = response; }); console.log(data); – Linus Juhlin Aug 06 '12 at 14:05
  • I've just mentioned it as an example. The catch here is to define date before your post code and set its value whenever response is received. – aemdy Aug 06 '12 at 14:06
  • Keep in mind that its value will be null unless the post request is done. – aemdy Aug 06 '12 at 14:07
0

The example you posted will do nothing EVERY TIME because AJAX calls like this are ASYNCHRONOUS (that's what the A in AJAX stands for).

If you want to be able to use the value of the output, add a hidden element to your page with a unique ID that contains the value. Then you can access that element through javascript.

Matt
  • 6,993
  • 4
  • 29
  • 50
0

Thats because, the ajax call is asynchronous. You should never return a value from a callback function. Do the job in the callback or trigger an event.

naden
  • 126
  • 8