Hello and welcome to our community! Is this your first visit?
Register
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
    Posts
    138
    Thanks
    3
    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:
    <?php

    include 'configure.php';

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

    $resmysql_query($qr);
    $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');
      echo 
    json_encode($stud_arr);
    ?>
    http://www.trialsevolution.co.uk/mob...php?trackid=71

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 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'); 

  • #3
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,280
    Thanks
    12
    Thanked 343 Times in 339 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
    •