数据库行没有删除,AJAX,PHP错误

时间:2018-02-18 02:46:58

标签: javascript php jquery html ajax

我想从HTML表中删除数据库行数据。我有一个HTML表,我已调用所有数据库表值。我想给用户一个删除订单选项。我已经使用了删除按钮。为此,我试图使用ajax和jquery与php删除查询。但问题是当我点击删除它时只删除HTML行数据。它也应该从DB中删除该特定的id行。我需要你的帮助。请让我知道我做错了什么?我非常喜欢ajax和jquery。

remove.php

<?php 
include "config.php";

$id = $_REQUEST['id'];

// Delete record
$query = "DELETE FROM mi_home_tv WHERE id='$id'";
mysqli_query($link,$query);

echo 1;

jquery+ajax code
<script src='jquery-3.0.0.js' type='text/javascript'></script>
<script type="text/javascript">
    $(document).ready(function(){

    // Delete 
    $('.delete').click(function(){
        var el = this;
        var id = this.id;
        var splitid = id.split("_");

        // Delete id
        var deleteid = splitid[1];

        // AJAX Request
        $.ajax({
            url: 'remove.php',
            type: 'POST',
            data: { id:deleteid },
            success: function(response){

                // Removing row from HTML Table
                $(el).closest('tr').css('background','tomato');
                $(el).closest('tr').fadeOut(800, function(){      
                    $(this).remove();
                });
            }
        });
    });
});
</script>

按钮代码:

echo "<td> <span  id='del_<?php echo $id; ?>'  type='submit' class=' delete btn c-theme-btn c-btn-uppercase btn-xs  c-btn-square c-font-sm'>Delete</span> </td>";

php代码:

 <form method="post" action="remove.php">
                                        <?php
                                       echo " <thead>
                                            <tr>

                                                <th>Order ID</th>
                                                <th>Date</th>
                                                <th>Name</th>
                                                 <th>Store Name</th>
                                                <th>Zip</th>
                                                <th>City</th>
                                                <th>Address</th>
                                                 <th>Contact</th>
                                                  <th>Tv Varient</th>
                                                  <th>Total</th>
                                                   <th>Delivery</th>
                                                  <th>Action</th>
                                            </tr>
                                        </thead>

                                              <tbody>";
                                               while($row = mysqli_fetch_array($result))
                                          {
                                             $id = $row['id'];

                                           echo"<tr>";
                                                echo "<td>" . $row['id'] . "</td>";
                                                echo "<td>" . $row['rdate'] . "</td>";
                                                echo "<td>" . $row['rname'] . "</td>";
                                                echo "<td>" . $row['rstore'] . "</td>";
                                                echo " <td>" . $row['rzip'] . "</td>";
                                                echo "<td>" . $row['rcity'] . "</td>";
                                                echo "<td>" . $row['raddress'] . "</td>";
                                                echo "<td>" . $row['rphone'] . "</td>";
                                                echo "<td>" . $row['rtv_varient'] . "</td>";
                                                echo"<td>" . $row['ramount'] . "</td>";
                                                echo"<td>" . $row['rdelivery'] . "</td>";

                                                echo "<td> <input type='submit' value='delete'  id='del_<?php echo $id; ?>' type='submit' class=' delete btn c-theme-btn c-btn-uppercase btn-xs  c-btn-square c-font-sm'>Delete</button> </td>";

                                                  echo"</tr>";


                                                 }
                                                 ?>
                                             </form>

1 个答案:

答案 0 :(得分:0)

您应该有一个表格,其输入如下:

<form method="post" action="remove.php">
<input type="submit" value="Delete" name="id" />
</form>