[英]How to delete on the trash icon glyphicon with php
I am generating a table with data. 我正在生成带有数据的表。 the table contains action. 该表包含动作。 and one of those actions is to delete a row from that table (and from the database). 这些动作之一就是从该表(和数据库)中删除一行。
But how can i manage to delete a row when i click on the Trash Glyphicon without moving to another page. 但是,当我单击“垃圾桶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ë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>
Put this in your <script>
tag at the very bottom of the page. 将其放在页面底部的<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();
}
}
});
});
Refer jquery.ajax for more ajax options. 有关更多ajax选项,请参考jquery.ajax 。
You need to handle many things like if the URL in ajax is same page, then in PHP you will have check if its the ajax request, echo just the data
instead of form, etc... 您需要处理很多事情,例如ajax中的URL是同一页,然后在PHP中您将检查其ajax请求,仅回显data
而不是表单等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.