Row selection and deletion but not from the database

Row selection and deletion but not from the database

Saad RasheedSaad 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';  
 }  
 ?>

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    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

  • Saad RasheedSaad Rasheed Posts: 3Questions: 1Answers: 0

    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!

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    You need to answer Colin's questions.

  • Saad RasheedSaad Rasheed Posts: 3Questions: 1Answers: 0

    yes, confirmed and the script doesn't generate any error.

  • allanallan Posts: 63,692Questions: 1Answers: 10,500 Site admin

    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

This discussion has been closed.