I have a PHP table whose data was streamed-out from the database using the foreach. However, i am trying to create a delete mechanism to it.
Here is my table :- <?php
foreach ($projects as $projectx){
echo '<tr>';
echo '<th>'.$projectx['id'].'</th>';
echo '<td>'.$projectx['user'].'</td>';
echo '<td>'.$projectx['catch_code'].'</td>';
echo '<td>'.$projectx['user_type'].'</td>';
echo '<td>'.$projectx['logs'].'</td>';
echo '<td><a href="view-details" class="btn btn-sm btn-primary view-files" data-id="'.$projectx['id'].'" data-user="'.$projectx['user'].'" data-catchcode="'.$projectx['catch_code'].'" >View</a> <i class="fa fa-trash" id="remove-id" class="btn btn-danger btn-sm remove" aria-hidden="true"></i>
';
}
This is what i have tried so far towards it:-
$('body').on('click', '.fa-trash', e => {
let id = $('td .view-files').data('id');
if(confirm("Do you want to Remove?")){
$.ajax({
url: '/del.php',
type: 'GET',
data:{id:id},
error: function(){
alert("something went wromg");
},
success: function(e){
e.preventDefault;
$("#"+id).parents('tr').remove();
}
});
}
});
Whenever i tried to delete any of the data from the table, the first data on the table is often deleted (not the intended data).
I think i am having a problem integrating/retrieving the font awesome delete class and the actual data through the IDs and classes. Please, how can i go about this?