Row selection and deletion but not from the database
Row selection and deletion but not from the database
Saad Rasheed
Posts: 3Questions: 1Answers: 0
Row selected and deleted but not deleted from the database because if we reload the page so the deleted data is back so, I want to select the row and delete from the page and database,both. Please, somebody, help me!
<script>
$(document).ready(function() {
var table = $('#example').DataTable();
$('#example tbody').on( 'click', 'tr', function () {
if ( $(this).hasClass('selected') ) {
$(this).removeClass('selected');
** if(confirm("Are you sure you want to delete this?"))
{
$.ajax({
url:"delete.php",
method:"POST",
data:{id:id},
dataType:"text",
success:function(data){
alert(data);
fetch_data();
}
});
} **
}
else {
table.$('tr.selected').removeClass('selected');
$(this).addClass('selected');
}
} );
$('#button').click( function () {
table.row('.selected').remove().draw( false );
} );
} );
</script>
And here is the code of delete file.
<?php
$connect = mysqli_connect("localhost", "root", "", "saad");
$sql = "DELETE FROM list WHERE id = '".$_POST["id"]."'";
if(mysqli_query($connect, $sql))
{
echo 'Data Deleted';
}
?>
This discussion has been closed.
Answers
Hi @Saad Rasheed ,
If it's going from the table, it looks like DataTables is doing it's bit.
Have you confirmed the correct data is being sent to the PHP script? Does the script generate an error? Does the correct row get deleted if you run the SQL by hand? Lots of things to try there.
Cheers,
Colin
Dear Colin,
Thanks for your reply.
Yes row deleted from the table I’m sure but not deleted from the database please have a look and guide me.
Thank you so much in advance!
You need to answer Colin's questions.
yes, confirmed and the script doesn't generate any error.
I'm surprised by that, since your Javascript uses a variable
id
(data:{id:id},
) but that isn't defined anywhere.It looks like you probably need to get the row id using
row().id()
.If you run into any other issues with it, please link to a test case showing the issue so we can offer further help.
Allan