Currently, I am working to develop a website to expand and sharpen my knowledge in web development, I am a beginner in programming.
So I am trying to display the data of my table named accountinfo in html datatable. I have tried to display it only in html datatable using php and echoed it in html file and it work perfectly fine. like this:
<table id="tblList" class="text-center">
<thead class="bg-light text-capitalize">
<tr>
<th hidden>User ID</th>
<th>Control No.</th>
<th>Account Name</th>
<th>Date Inspection</th>
<th>Date of Report</th>
<th>Actual Use</th>
<th>Landmark</th>
<th>Registered Owner</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php
echo $output;
?>
</tbody>
</table>
This code has no errors, but i want to use the javascript to display it in the data table.
But i dont have any idea on how to work on it..
I have tried it a little but i did not get the right result. it does not display the value of the Mysql Table in HTML data table.
I tried this:
function loadAccountList() {
$.ajax({
type: 'POST',
url: '../php_functions/tableList.php',
dataType: 'json',
data: xdata,
contentType: 'application/json; charset=utf-8',
success: function (response) {
$('#tbodyemplist').empty();
var cells = eval("(" + response.d + ")");
for (var i = 0; i < cells.length; i++) {
var resDate;
if (cells[i].res_date == "")
{
resDate = "N/A";
}
else
{
resDate = cells[i].res_date;
}
$('#tbodyemplist').append('<tr data-empid="' + cells[i].ID + '">'
+ '<td style="font-size:11px; text-align:center;" hidden>' + cells[i].badgenum+ '</td>'
+ '<td style="font-size:11px; text-align:center;">' + cells[i].CN+ '</td>'
+ '<td style="font-size:11px; text-align:center;">' +cells[i].AccName+ '</td>'
+ '<td style="font-size:11px; text-align:center;">' + cells[i].DI+ '</td>'
+ '<td style="font-size:11px; text-align:center;">' +cells[i].contact_no+'</td>'
+ '<td style="font-size:11px; text-align:center;">' + cells[i].DR+</td>'
+ '<td style="font-size:11px; text-align:center;">' + cells[i].ActUse+ '</td>'
+ '<td style="font-size:11px; text-align:center;">' + cells[i].Landmark+ '</td>'
+ '<td style="font-size:11px; text-align:center;">' + cells[i].status+ '</td>'
+ '</tr>');
}
},
error: function (error) {
console.log(error);
},
complete: function () {
$('#tblemplist').dataTable({
"order": [],
"columnDefs": [{
"targets": 'no-sort',
"orderable": false,
}]
}).end();
$('#tblemplist').css('width', '100%');
}
});
}
And call it in html like this
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="jsFiles/index.js"></script>
and also the php file
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "benchmark";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$acc_id="";
$acc_name="";
$date_inspection="";
$date_report = "";
$act_useof_prop="";
$landmark="";
$reg_owner="";
$bvcsi_control = "";
$status = "";
$lstrow = "";
$sql = "SELECT account_id, account_name, date_inspection, date_report, act_useof_prop, landmark, reg_owner, bvcsi_control,status from accountinfo ORDER BY account_id ASC";
$result = mysqli_query($conn,$sql);
$num = mysqli_num_rows($result);
$output = '';
if ($num > 0)
{
while ($row = mysqli_fetch_array($result))
{
$ID= $row["account_id"];
$CN= $row["bvcsi_control"];
$AccName= $row["account_name"];
$DI= $row["date_inspection"];
$DR= $row["date_report"];
$actUse= $row["act_useof_prop"];
$landmark = $row["landmark"];
$reg_owner = $row["reg_owner"];
$status = $row["status"];
}
}
?>
This project will be given to the company I had been render my OJT.
Edit
Please see attached image: Console error:
success: function (response)try toconsole.log(response)it is show data in yourconsole(inspect element browser and choose console tab)?