My problem should be very easy to resolve, I just can't get it together.
I have a simple working php file. It retrieves data from a database and assigns it to an array. It does not requires parameters and such. It gives a straight array:
PHP
...PDO connection code...
...SQL code....
$query = $dbc->query($sql);
$db_data = $query->fetchAll();
//I added the code below to send to JQuery:
echo json_encode($db_data);
No problems here, it gives me a nice multidimensional array
The problem is in my JQuery:
$(document).ready(function () {
var ajaxURL = 'ajax.php';
var ajaxData = {};
$.get(ajaxURL, function (data) {
ajaxData = data;
});
//For testing
alert( ajaxData );
//JQuery code to be used with ajaxData...
});
$.get
is retrieving the array, without problems. data
is 100% OK, but...
When I want to use ajaxData
it's empty or undefined (if I remove var ajaxData = {};
). I need ajaxData
to get the contents of $db_data
to use on other stuff latter on.
The core of the problem is:
JQuery loads and runs all the other code first (Past $.get
). And THEN, it runs $.get
code last. How can I get it to run $.get
FIRST, and then to run whatever code is below? Is that what you call a synchronous ajax request?