views:

71

answers:

1
 while ($row = mysql_fetch_object($result)) {
    $data[] = $row;
    echo "<div id='captionbox' style='width: 110px;float:left;color:#FFF;text-align:center;'>";
    echo "<a href='#' class='thumb'><img class='thumb-img' value = ".$row->aid." onclick='getVote(".$row->aid.", \"".$row->atitle."\")' src='images/roadies/th".$row->aid.".jpg' /> </a>";
    echo "<input  type = hidden name = aid id = rd".$row->aid." value = ".$row->aid.">".$row->atitle."</input>";
    echo "</div>";
    }

    $jsfriend = json_encode($data);

the above is my php code. i want the $data to be a JSON object. now i want to import it in my javascript file and use as a JSON object by using Jquery. But I am completely lost in the docs. there is a PHP example for doing things, but that includes some #id example i dont understand. can someone build me a Jquery function for this?

thanks a lot in advance.

*EDIT - What I am trying to do * I am trying to get the JSON object jsfriend in to my javascript file, script.js. I dont know how to use the Jquery function. If someone could explain it. Also tell me once the JSON object is in the javascript, how do i access values from the JSON object?

EDIT 2: my JSON string reads like this:

[{"aid":"1","atitle":"Ameya R. Kadam"},{"aid":"2","atitle":"Amritpal Singh"},{"aid":"3","atitle":"Anwar Syed"},{"aid":"4","atitle":"Aratrika"},{"aid":"5","atitle":"Bharti Nagpal"}]

can someone tell me the Javascript if I want to display the name for aid:"1" ?

+1  A: 

A JSON string can be used directly in javascript.

echo '<script type="text/javascript">';
echo 'var foo = '.json_encode($data);
echo 'alert(foo[0].column_name);';
echo '</script>';
Bob Fanger
Yet in few years when IE6 will be dead it would be much more secure to use `var foo = JSON.parse(data);`.
vava