jquery JSON php
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>";
开发者_如何学Go 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" ?
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>';
精彩评论