Question: ```
$.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. :(

Answer: 
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);
                        });

```
```
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.