I'm trying to create a variable for each row returned by query of my database. I've tried the following, and only get a result of [] for users. The php function should be returning 5 unique rows. I've read and reread the linked topic (How do I return the response from an asynchronous call?) and get lost on the callbacks. I've updated my code and think I'm getting closer, but am still not there.
$(document).ready(sources);
var source = [];
// Get the user data to build the sources
function getUsers(data) {
$.ajax(
{
url: '/s/calendar_userdata.php',
method: "GET",
success: data // is this the correct use of a callback?
})
};
// create the calendar sources
function calSource() {
getUsers(function(users){
var length = users.length; // Length is 2. Should be 5.
for(var i = 0; i < length; ++i)
{
source[i] = '/s/events.php?e=' + users[i][userid];
}
return source;
});
};
My ultimate objective is for the for loop to output something like the following for the javascript to use.
// Assuming 3 rows of user data
source[0] = '/s/events.php?e=1';
source[1] = '/s/events.php?e=2';
source[2] = '/s/events.php?e=3';
Thanks. I appreciate the assistance.
I now understand the asynchronous nature of ajax calls. Thanks for that.