troubleshooting Question

json_encode returns undefined values

Avatar of Colin Brazier
Colin BrazierFlag for United Kingdom of Great Britain and Northern Ireland asked on
JavaScriptPHPJSON
11 Comments1 Solution125 ViewsLast Modified:
Why am I getting values Undefined in this page?

http://www.col-b.uk/json/

It is using json_encode.

I am debugging via the console and the array seems fine but individual values are undefined - see screen grab js.jpg.

Here's the code of script.js
$(document).ready(function(){
    $.ajax({
        url: 'ajaxfile.php',
        type: 'get',
        dataType: 'JSON',
        success: function(response){
			
            var len = response.length;
				
            for(var i=0; i<3; i++){
                var id = response[i].id;
                var username = response[i].username;
                var name = response[i].name;
                var email = response[i].email;
				
				console.log(response);
				
				console.log(response[i].username);

                var tr_str = "<tr>" +
                    "<td align='center'>" + (i+1) + "</td>" +
                    "<td align='center'>" + username + "</td>" +
                    "<td align='center'>" + name + "</td>" +
                    "<td align='center'>" + email + "</td>" +
                    "</tr>";

                $("#userTable tbody").append(tr_str);
            }

        }
    });
});

[embed=file 1446456]
js.jpg
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 11 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 11 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros