I am currently working with a datatable in my project, but I'm facing difficulty in retrieving data from my ajax file to the datatable codes.
Below are the snippets of my codes:
AJAX
<?php
function functionClass($id) {
// create sql query
$Class = "SELECT class.id, class.day, class.start_time, class.end_time, subject.subject_name, tutor.tutor_id, tutor.tutor_name AS t_name, classroom.room
FROM class, subject, tutor, classroom
WHERE class.subject_id = subject.id
AND class.tutor_id = tutor.id
AND class.classroom_id = classroom.id";
//$resultClass = mysqli_query($link, $viewClass) or die(mysqli_error($link));
}
?>
Javascript
<script>
`$(document).ready( {`
$.fn.dataTable.TableTools.defaults.aButtons = [ "csv" ];
$('.datatable').DataTable({
responsive: true,
serverSide: true,
processing: true,
lengthMenu: [ [10, 25, 50, -1], [10, 25, 50, "All"] ],
ajax: {
url: "http://localhost/FYP/dt/class/",
type: "POST",
data: {
method: "get"
}
},
columns : [
{
data: "id",
render: function(data, type, row) {
return <?php echo $row['id'] ?> + row.id + '/">' + data + '</a>';
}
}
]
});
</script>
My datatable method is
<div class="container">
<div class="btn-toolbar" role="toolbar">
<a href="addNewClass.php" class="btn btn-success"><span class="fa fa-fw fa-plus"></span> New Class</a>
</div>
<table class="table table-hover table-striped datatable" width="100%">
<thead>
<tr>
<th class="all">S/N</th>
<th class="min-tablet-l">Day</th>
<th class="min-tablet-l">Start Time</th>
<th class="min-tablet-l">End Time</th>
<th class="min-tablet-l">Subject</th>
<th class="min-tablet-l">Staff ID</th>
<th class="min-tablet-l">Tutor</th>
<th class="min-tablet-l">Classroom</t
</tr>
</thead>
<tbody>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tbody>
<tfoot>
<tr>
<th>S/N</th>
<th>Day</th>
<th>Start Time</th>
<th>End Time</th>
<th>Subject</th>
<th>Staff ID</th>
<th>Tutor</th>
<th>Classroom</th>
</tr>
</tfoot>
</table>
</div>
UPDATE: After resolving most of the issues, I encountered an error when initializing the table, stating "3. Warning: Cannot reinitialize Data." Upon checking my console, it indicates that POST http://localhost/FYP/dt/functionClass/ could not be found (404 Error).