mercredi 22 juin 2016

table row delete in database without reloading the page

I want to delete row in database without reloading the page. I have written following codes. It does delete row in page but not in database. which means connection not going to ajax. pls help.

    <?php  


$advances_result= mysqli_query($conn, "SELECT * FROM advances") or die("error");
?>

        <table border>
            <caption>ADVANCES</caption>
        <tr>
            <th>Date</th>
            <th>EmpName</th>
            <th>EmpCode</th>
            <th>Amount</th>
            <th>Comment</th>
            <th>Action</th>
        </tr>

<?php
while($row = mysqli_fetch_array($advances_result)){
?>    
    <tr>
    <td><?php echo $row['date'];?></td>
    <td><?php echo $row['empname'];?></td>
    <td><?php echo $row['empcode'];?></td>
    <td style='text-align:right'><?php echo $row['amount'];?></td>
    <td><?php echo $row['comment'];?></td>
    <td><button class="delete" id="<?php echo $row['id']; ?>" > Delete</button></td>
    </tr>


<?php    
}
?>
        </table>

'jquery part'

                        <script>
                        $(document).ready(function(){                
                        $(".delete").click(function() {


                         if (confirm("Do you want to delete this row?"))
                                        {
                                            var row = $(this).parents('tr');
                                            var id = row.attr("id");
                                            var data = 'id=' + id ;

                        $.post({
                           type: "post",
                           url: "deleteadvances.php",
                            data: data,
                           cache: false,
                           success: function(){ 
                            row.slideUp('slow', function() {$(row).remove();});

                          }
                         });
                         }
                        return false;
                            });
                        });
                        </script>

'deleteadvances.php' page:

<?php
include("connection.php");

$id = $_POST['id'];
mysqli_query($conn,"INSERT INTO advancehistory SELECT * FROM advances WHERE ID='".$id."'");
mysqli_query($conn,"DELETE FROM advances WHERE ID='".$id."'");



?> 

Aucun commentaire:

Enregistrer un commentaire