I have an array of elements from my database. I used ajax (POST procedure) to get it. Now I want to create a table, which will contain those elements. I don't exactly know how to do it in jQuery. I've got this:
$.post('script.php',{login: '1'}, 'json').done(function(data) {
results = '';
data.table.forEach(function(row){
results += '<tr><td>' + row.name + '</td><td>' + row.lastname + '</td><td>' + row.data + '</td></tr>';
});
$('#MyTable tbody').html(results);
});
My script look's like:
header('Content-Type: application/json');
...
$result=mysql_query($query);
$table = array();
while($row=mysql_fetch_array($result)){
$table[] = $row;
}
$data['table'] = $table;
echo json_encode($data);
Edit: I corrected my mistakes and Now I have errors in console when I open table. It displays:
TypeError: data.forEach is not a function
Does somebody know how the structure of this sentence should look ?
tablein that array. try justdata.forEach(... Sample of whole json structure would helprow.lastanmeshould berow.lastname. But that won't cause this problem.