0

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.

Community
  • 1
  • 1
SteveSTL
  • 998
  • 3
  • 13
  • 21
  • You're using the result of `getUsers`, but it doesn't return anything. – Barmar Apr 10 '14 at 02:29
  • @OneKitten: Thanks. Reading that now. – SteveSTL Apr 10 '14 at 02:32
  • @Barmar: How do I make it return the values? My php file ends with echo json_encode($results); I figured that returned the result. Thanks. – SteveSTL Apr 10 '14 at 02:35
  • Read the duplicate question. We linked to it for a reason. – Barmar Apr 10 '14 at 02:37
  • Yes, that returns the results from PHP to the browser. But you have no `return` statement in `getUsers`. And even if you did, it wouldn't work because AJAX is asynchronous. This is all explained in the other question. – Barmar Apr 10 '14 at 02:38

0 Answers0