i have this code:
<script>
$(document).ready(function()
{
refresh();
});
function refresh()
{
$.get('getMessageDetails.php', function (json) {
alert(json); //as a test for now
});
}
</script>
then i have my getMessageDetails.php:
<?php
header('Content-Type: application/json; charset=utf-8');
include('header_application.php');
$lastNewMessageCnt = $obj_clean->getUnopenedMessagesCount($_SESSION['user_id']) + 1;
$last_unopened_message_row = $obj_clean->getLastUnopenedMessage($_SESSION['user_id'],$lastNewMessageCnt);
echo json_encode($last_unopened_message_row);
?>
and my html:
echo "<tr>";
echo '<td><a id = "message_id" class="red_link" href="'.ADDRESS.'view_message.php?id='.$r['id'].'"><span id = "subject">'.$r['subject'].'</span></a></td>';
echo '<td id = "unique_code1">'.$uniqueCode1.'<span id = "unique_code2" class="pink_text">'.$uniqueCode2.'</span><span id = "unique_code3">'.$uniqueCode3.'</span></td>';
echo "</tr>";
how come when i alert(json) i get the values im supposed to get which is the last message:
[{"subject":"Freechat ha ha","id":"15","created_at":"2011-08-29 11:16:45","unique_code":"LUCINEM000RC","opened_once":"0"}]
BUT as soon as i code this:
alert($("#subject").html(json.subject));
alert($("#message_id").html(json.id));
alert($("#unique_code1").html(json.unique_code));
i get the first message’s details??? which is not what i want
where am i going wrong please??
thank you
in JSON
{}[]so to access the requred data, you need to do so..
DEMO
UPDATE
actually there is problem in ur jquery function
either use jQuery.getJSON()
or if you are using jQuery.get then write
"json"as an third argument like thisNOTE: why don’t you install firebug, that will tell your error runtime