I have written php code to generate json data, Now We have to display this json data into table. My php code is able to generate data but not able able to insert into ta
This is a way to work this with JavaScript
function fetchData1(){
$(".data-contacts1-js tbody").empty();
$.get("http://localhost/service/newJobs.php", function(data) {
data=JSON.parse(data); //if the server returns text instead JSON object
for(var i in data){
var tr=$("<tr></tr>");
tr.append("<td>" + data[i].cust_name + "</td>" +
"<td>" + data[i].cust_mobile + "</td>" +
"<td>" + data[i].cust_email + "</td>" +
"<td>" + data[i].cust_address + "</td>");
$(".data-contacts1-js tbody").append(tr);
}
});
}
$(document).ready(function(){
$(".data-contacts1-js tbody").empty();
$('#fetchContacts1').click(function() {
fetchData1();
});
});
On server script (php) add in first row
header('Content-type: application/json');
isn't php an option to print the table? If yes you can just echo table rows in the foreach loop and that's it:
while( $row = mysql_fetch_array( $result ) ) {
$retVal[] = $row;
}
becomes something like
while( $row = mysql_fetch_array( $result ) ) {
$table_row .= "
<tr>
<td>$row['cust_name']</td>
<td>$row['cust_mobile']</td>
<td>$row['cust_email']</td>
<td>$row['cust_address']</td>
</tr>";
}