0

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."'");



?> 
  • First you can check the return of your `mysqli_query()` to see what it returns. Second you have to protect your queries from SQL injection (by preparing & bind_param your parameters) – jquiaios Jun 19 '16 at 11:41
  • You don't really need `type: 'post'` when using the `$.post()`-method. – M. Eriksson Jun 19 '16 at 11:53

3 Answers3

0

You need to test the output of mysqli_query in deleteadvances.php, if all good, it shows JSON Ok message that is easy parsable with $.post on the jQuery side.

e.g: {"deleted":"id"} on success or {"error":404} on error not found.

user229044
  • 232,980
  • 40
  • 330
  • 338
louffi
  • 215
  • 5
  • 19
  • how to test it... i am beginner and have only basic knowledge in coding.. – Shihab Panalam Jun 20 '16 at 04:44
  • add response codes [here](http://stackoverflow.com/questions/3351882/convert-mysqli-result-to-json) and you need to added `jQuery.parseJSON()` to your jQuery part as described [here](http://stackoverflow.com/questions/10475652/jquery-post-processing-json-response) – louffi Jun 20 '16 at 04:55
0

I think the id that you're passing from javascript to ajax is incorrect.

var data = 'id=' + id;

is appending the id with string 'id=' and now it finally becomes a string. When it is being passed to ajax, there is just a value and not a [key, value] pair. So, when you are accessing it in php you will get wrong id or Null value.

Try:

$.post({
    type: "post",
    url: "deleteadvances.php",
    datatype : 'json',
    data: {
      id : id
    },
    cache: false,
    contentType : 'application/x-www-form-urlencoded; charset=utf-8'
});
Agam
  • 1,109
  • 9
  • 10
0

Change 'deleteadvances.php' page:

<?php

    include ('connection.php');

    $id=$_POST['id'];
    $delete = "DELETE FROM table_name WHERE id=$id";
    $result = mysqli_query($db,$delete) or die("Bad SQL: $delete");

?>
Viraj Tharinda
  • 29
  • 2
  • 10