Adding onClick Functionality to the render function.
Adding onClick Functionality to the render function.
On columnDefs target 6, on the dropdown link I have a link that deletes the selected id usig the data, Currently it sends the stid to the delete_student.php page, thats cool but I cant catch errors. What I want is to add a onclick function on the link that triggers a function for delete and triggers a notification (I use iziToast for notification).
Below is my code for fetching all the results using ajax and rendering the returned array.
let table = $('.datatable-ajax').DataTable({
ajax: {
'url': 'queries/get_all_students.php',
"type": "GET",
"dataSrc": "",
"processing": true,
"serverSide": true,
},
cache: true,
"columnDefs": [
{
"targets": 0,
"data": "StudentName",
}, {
"targets": 1,
"data": "RollId",
}, {
"targets": 2,
"data": "RegDate",
}, {
"targets": 3,
"data": "ClassName",
"render": function (data) {
return '<a href="../class/pg/class_exams.php">' + data + '</a>'
}
}, {
"targets": 4,
"data": "age",
}, {
"targets": 5,
"data": "Status",
'render': function (data) {
if (data == 1) {
return '<span class="label label-flat border-success text-success-600">Active</span>'
} else {
return '<span class="label label-flat border-danger text-danger-600">InActive</span>'
}
}
}, {
"targets": 6,
"data": "StudentId",
"render": function (data, type, row, meta) {
return '<ul class="icons-list">' +
' <li class="dropdown">'+
' <a href="#" class="dropdown-toggle" data-toggle="dropdown" aria-expanded="false"> <i class="icon-menu9"></i> </a>' +
' <ul class="dropdown-menu dropdown-menu-right">'+
'<li><a href="pg/class_exams.php?cid='+data+'">View</a></li>'+
'<li><a href="pg/edit_class.php?cid='+data+'">Edit</a></li>'+
'<li><a href="pg/edit_class.php?cid='+data+'">Make InActive</a></li>'+
'<li><a id="delete_student" href="queries/delete_student.php?stid='+data+'" >Delete</a></li>' +
'</ul> ' +
'</li>' +
'</ul>'
},
}
],
});
Below is the delete_student.php code snippet..
$delete = $_GET['stid'];
$query = "DELETE FROM `tblstudents` WHERE StudentId = '$delete'";
$result = mysqli_query($con, $query) or die(mysqli_error($con));
if ($result) {
echo '
<script>document.location = "../index.php";
console.log("Deleted");
</script>';
}else{
echo '
<script>document.location = "../index.php";
console.log("Not Deleted");
</script>';
}
Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.