0

I'm trying to run a simple getJSON to a PHP file using:

$.getJSON("loadThumbs.php", { usrID: 25 }, function(data){
  alert(data.filename);
  });

And when the alert messages pops up it reads "undefined."

Here's my PHP file (loadThumbs.php):

$usrID = $_GET['usrID'];

$sql = "SELECT id, isDefaultProfile, filename, usrID FROM profile_images WHERE   isDefaultProfile=1 AND usrID='$usrID'";
$result = mysql_db_query($DBname,$sql,$link) or die(mysql_error()); 

$rows = array();

while($r = mysql_fetch_assoc($result)) {
   $rows[] = $r;
 }
print json_encode($rows);

//Which outputs: [{"id":"5","isDefaultProfile":"1","filename":"26.jpg","usrID":"25"}]

Any ideas on what I might be doing wrong?

1
  • Are you sure that the usrID field is making its way to the server? Try rewriting the {usrID: 25} line to {"usrID": 25}. Commented Sep 19, 2009 at 17:34

1 Answer 1

5

Try:

alert(data[0].filename);

The JSON being returned is an array (the brackets) containing one object (the curly braces), so you have to access the first element of the array to be able to get the filename of the first file.

Sign up to request clarification or add additional context in comments.

1 Comment

That was it. Makes sense now. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.