Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Apr 2013
    Thanked 0 Times in 0 Posts

    Question JSON outputs as Undefined?

    Hi, my code below converts a result into JSON fine as you can see from the link below but I am gettiing 'UNDEFINED' in my page

    As it is an array does it need [0] somewhere?!

    PHP Code:

    include 'configure.php';

    $stud_id $_GET["trackid"];
    $qr "SELECT * FROM trialstracks WHERE trackid = $stud_id";

    $row mysql_fetch_array($res);
    $stud_arr["trackid"] = $row["trackid"];
    $stud_arr["trackname"] = $row["trackname"];
    $stud_arr["trackdifficulty"] = $row["trackdifficulty"];
    $stud_arr["trackcreator"] = $row["trackcreator"];
    $stud_arr["tracktype"] = $row["tracktype"];
    $stud_arr["trackreleased"]= $row["trackreleased"];
    $stud_arr["trackbio"] = $row["trackbio"];
    $stud_arr["trackpic"] = $row["trackpic"];
    $stud_arr["trackvideo"] = $row["trackvideo"];

    header('Access-Control-Allow-Methods: POST, GET, OPTIONS'); // set the methods you want
    header('Access-Control-Allow-Origin: *'); // this allows access from any domain
    header('Content-type: application/json');

  2. #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Saskatoon, Saskatchewan
    Thanked 2,668 Times in 2,637 Posts
    Since there's no error handling in here, there are plenty of opportunities to trigger undefined in PHP. You'll need to be more specific on the error.

    Given the link you have though, there's no problem here. I see a JSON array as your results, so that says there's nothing wrong.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  3. #3
    Master Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Behind the Wall
    Thanked 568 Times in 561 Posts
    As it is an array does it need [0] somewhere?!
    I’m not sure how you call that JSON in JS, but be aware that this JSON is an array, which only contains a single object, i.e property_value = json[0][property_name]

    however, according to your output the code above is not the one creating the JSON (no hint of a wrapping array).

    additional note, PHP associative arrays translate into JS plain objects, not JS arrays.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts