Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7920629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:17:27+00:00 2026-06-03T16:17:27+00:00

I think this is more down to timing than code, so really I am

  • 0

I think this is more down to timing than code, so really I am looking for best practice advice on how best to get a JSON response.

<script type="text/javascript">

            $(window).load(function() {
                $('#messages').append('<img src="images/loading.gif" alt="Currently Loading" id="loading" />');

                var ideaid = <?php echo $_GET['ideaid']; ?>;
                $.ajax({

                    url: 'sql/ajaxsql.php',
                    type: 'POST',
                    data: 'switch=commentList&ideaid=' + ideaid + '&filter=sortdate',
                    dataType: 'json',

                    success: function(result) {
                        var len = result.length;
                        var html;
                        console.log('length= ' + len);
                        $('#response').remove();
                        console.log(result);
                        for(var i = 0; i < len; i++) {
                            var pic = '<img src="https://graph.facebook.com/' + result[i].user_id + '/picture&type=small" align="middle" />';
                            var authname;
                            FB.api('/' + result[i].user_id + '?fields=name', function(AuthName) {
                                console.log(AuthName);
                                alert(AuthName.name);
                               authname = AuthName.name;

                            });
                            html = '<p>' + result[i].comment + '<br><hr>Date Added: ' + result[i].date + ' by ' + pic + ' ' + authname + '<br><hr><hr></p>';
                            $('#comms').append(html);

                        }

                        $('#loading').fadeOut(500, function() {
                            $(this).remove();

                        });
                    }
                });

                return false;
            });

        </script>

With this code, it fires off to get comments regarding a certain idea (idea_id). The comments only holds the Users ID (facebook). When all the data is back, the success then sorts the data ready to print to the screen in some order.

As part of the success, I have the date, time, FB image and name as part of the Author Info under each comment.

Date and Time, works. Image using the graph works, but the name is a bit late of the window loading, and so misses it’s call, so comes back as undefined and then the Alert pops up with the name. I understand ajax is meant to do this.

Whats the best way to get round this.

Thank you in advance.

Andrew

EDIT

I have been unable to make this work, even with the suggestions below.

EDIT AGAIN Just seen bf new updated version as below. would also have worked. But I have spent a day on this one function and dare not to play.

As soon as the FB.api comes into play, I could not get the values from out side it. So I took a different approach.

Rather than ajax, I used the query from the PHP side that gets the data, including the uid and then json queried that, and bolted it onto the (mysql_fetch_array) array as follows:

         $gc_result = mysql_query($gc_query);
    while ($result = mysql_fetch_array($gc_result)) {
        $jsonURL = "https://graph.facebook.com/" . $result['user_id'] . "/";
        $json = json_decode(file_get_contents($jsonURL), true);
        $result["name"] = $json['name'];

        $data[] = $result;
    }

    echo json_encode($data);

Now I have that, I can then do the following and call it within the jQuery:

for(var i = 0; i < len; i++) {
    var pic = '<img src="https://graph.facebook.com/' + result[i].user_id + '/picture?type=small" align="middle" />';
     html = '<p>' + result[i].comment + '<br><hr>Date Added: ' + result[i].date + ' by ' + pic + ' ' + **result[i]['name']** + '<br><hr><hr></p>';
     $('#comms').append(html);  
                        }

This all works great, and I am a complete novice to programming jquery and using Facebook API and JSON, but even I sit back and am pretty impressed with this solution. Before I get carried away, are there any potential flaws in this, performance or security wise ???

Thanks again in Advance.

Andrew

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T16:17:30+00:00Added an answer on June 3, 2026 at 4:17 pm

    The call to FB.api is probably asynchronous (another ajax request), so you have to move the code after it to inside the FB.api callback:

    FB.api('/' + result[i].user_id + '?fields=name', function(AuthName) {
        console.log(AuthName);
        alert(AuthName.name);
        authname = AuthName.name;
        html = '<p>' + result[i].comment + '<br><hr>Date Added: ' + result[i].date + ' by ' + pic + ' ' + authname + '<br><hr><hr></p>';
        $('#comms').append(html);
    });
    

    You also have a variable scope problem because of the for loop. One of the ways to fix this is to use a separate function to create the callback. Add this right after your $(window).load block, before </script>:

    function createFbApiCallback(jsonResult) {
        return function(AuthName) {
            var authname = AuthName.name;
            var pic = '<img src="https://graph.facebook.com/' + jsonResult.user_id + '/picture&type=small" align="middle" />';
            var html = '<p>' + jsonResult.comment + '<br><hr>Date Added: ' + jsonResult.date + ' by ' + pic + ' ' + authname + '<br><hr><hr></p>';
            $('#comms').append(html);
        }
    }
    

    Then change your loop to this:

    for(var i = 0; i < len; i++) {
        FB.api('/' + result[i].user_id + '?fields=name', createFbApiCallback(result[i])); 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is more of a concept question than a code question, but I think
I think this is more like Math question rather than programming but since I'm
Does anyone actually think this is a good reason to Dumb down your code?
[see later answer for more] I think this is just a simple rails question,
Sorry if this is better suited at serverfault, but I think it learns more
I'm showing the code to this problem for example purposes, but really my question
I don't really know what more to say about this. I have never, ever
This is really a follow-up from this question , as I think I've solved
I think this is likely to be a generic .NET assembly loading question, but
I THINK this line is causing a 3.5K Byte memory leak, any ideas why?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.