如何使用 php 删除垃圾桶图标上的字形

How to delete on the trash icon glyphicon with php

我正在生成一个 table 数据。 table 包含动作。其中一个操作是从 table(和数据库)中删除一行。

但是,当我点击 Trash Glyphicon 而不移动到另一页时,我如何设法删除一行。

<form action ="" method="POST">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th>#</th>
                            <th>Categorie naam</th>
                            <th>Afbeelding</th>
                            <th>Action</th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php
                        $sqli = "SELECT * FROM category ORDER BY category_id";
                        $result = $connect->query($sqli);
                        if($result->num_rows < 0){
                            echo "<div class='alert alert-warning' role='alert'><strong>Oh oh! </strong>Er zijn geen categori&euml;n. <br></div>";
                        }
                        else{
                            while ($row=mysqli_fetch_assoc($result)){
                                $category[] = $row;
                            }
                            foreach($category as $value){
                                echo '<tr>';
                                echo    '<td>'.$value['category_id'].'</td>';
                                echo    '<td>'.$value['category_name'].'</td>';
                                echo    '<td>'.$value['c_filename'].'</td>';
                                echo    '<td>';
                                echo        '<a href="../../cms/content/category_management.php?page=alter_category&id='.$value['category_id'].'"><span class="glyphicon glyphicon-pencil"></span></a> / ';
                                echo        '<a href="../../cms/content/category_management.php?delete_category.php&id='.$value['category_id'].'" data-toggle="modal" data-target="#confirm-delete"><span class="glyphicon glyphicon-trash" id="submit" name="submit"></span></a>';
                                echo    '</td>';  
                                echo '</tr>';
                            }       
                        }
                    ?>
                    </tbody>
                </table>
            </form> 

将其放入页面底部的 <script> 标记中。

$(".glyphicon-trash").click(function(){

    $.ajax({
      url: "http://url/to/php/page.php",
      type: "POST",
      success: function( data ) {
        // From the response in data, you should be able to
        // know if delete was successful
        console.log(data);
        // your js code to remove the row
        // even I am not good with js/jquery
        if(data.success){ // example
          this.closest("tr").hide();
        }
      }
    });

});

参考 jquery.ajax 更多 ajax 选项。

你需要处理很多事情,比如如果 ajax 中的 URL 是同一个页面,那么在 PHP 中你将检查它是否是 ajax 请求,仅回显 data 而不是表格等...