This follows on from a previous question of mine but has been refocused to address a specific jQuery issue.
I am currently trying to parse JSON results from a PHP / MySQL search into a simple HTML table. However, no data is returned into my table when using the search. I am getting a response from the Ajax request as when I debug my search page in Firebug my request is returned in this correct JSON string:
[{"ARTIST":"Coldplay","TRACKTITLE":"Fix You","PLAYLIST":"Rock"},{"ARTIST":"Katy Perry","TRACKTITLE":"Roar","PLAYLIST":"Pop"}]
Where in my jQuery file can I parse the JSON to append each string of {}
data into a row of an HTML table (with ARTIST, TRACKTITLE and PLAYLIST as my column headers). I'm unclear as to where i'm going wrong and why my JSON string is not being converted into my HTML table correctly, so any pointers in the correct way to parse this data would be helpful.
jQuery file
$.post('ajax/name.php', {name: name}, function(data) {
var sentData=JSON.parse(data)
for(var i = 0; i < sentData.ARTIST.length; ++i)
var tr = $('<tr/>');
tr.append("<td>" + sentData.ARTIST + "</td>");
tr.append("<td>" + sentData.TRACKTITLE + "</td>");
tr.append("<td>" + sentData.PLAYLIST + "</td>");
$('#multiple').append(tr);
});
HTML extract
<table class="table hovered" id="multiple">
<thead>
<tr>
<th class="text-left">Artist</th>
<th class="text-left">Track Title</th>
<th class="text-left">Playlist</th>
</tr>
</thead>
</table>
Update
I added header('Content-Type: application/json');
into my PHP file and now Firebug is displaying a JSON tab, which is good news. However I still can't parse that JSON into the HTML table..