Good evening; I hope you can help me, I have a small problem when displaying the data within a datatable, the data if they are shown in the index but not within the datatable.
This is the JS code inside my index.php
$(document).ready(function(){
$.ajax({
url: 'getjson.php',
dataType: 'json',
})
.done(function(data){
console.log(data);
$.each(data.datos, function(i,post){
var newRow =
"<tbody>"
+"<tr>"
+"<td>"+post.login+"</td>"
+"<td>"+post.email+"</td>"
+"<td>"+post.nombre+"</td>"
+"<td>"+post.apellido+"</td>"
+"<td>"+post.cargo+"</td>"
+"<td>"+post.direccion+"</td>"
+"<td>"+post.gerencia+"</td>"
+"<td>"+post.jefatura+"</td>"
+"<td>"+post.jefe_inmediato+"</td>"
+"</tr>"
+"</tbody>";
$(newRow).appendTo("#json-data");
})
.fail(function(){
alert('Error');
})
});
But being called from the table with the id="json-data" the data loads it outside the datatable.
This is my getjson.php file in which I get the data from the bd
<?php
require_once 'dbconfig.php';
$posts = array();
$query = ("SELECT * FROM user ");
$stmt = $db_con->prepare($query);
$stmt->execute();
while($row=$stmt->fetch(PDO::FETCH_ASSOC)) {
//$posts['datos'][] = $row;
$posts['datos'][] = array_map("utf8_encode", $row);
}
echo json_encode($posts);
?>
and finally this is the code of the table
<table id="json-data" class="table table-striped table-bordered" >
<thead>
<tr>
<th> Login </th>
<th> Email </th>
<th> Nombre </th>
<th> Apellidos </th>
<th> Cargo </th>
<th> Dirección </th>
<th> Gerencia </th>
<th> Jefatura </th>
<th> Jefe Inmediato </th>
</tr>
</thead>
<tbody>
</tbody>
</table>
I hope you can help me.