Php 使用jqueryajax删除mySQL表行
我正在尝试这样做,当我点击一个span图标时,它会将文章id发送到我的php sql页面,删除我的文章,我使用jQuery Ajax发送id,id在jQuery端发送正常,但在http post请求完成后,我的表行仍然存在,有人能看到我的代码是否有问题,提前谢谢Php 使用jqueryajax删除mySQL表行,php,jquery,ajax,post,mysqli,Php,Jquery,Ajax,Post,Mysqli,我正在尝试这样做,当我点击一个span图标时,它会将文章id发送到我的php sql页面,删除我的文章,我使用jQuery Ajax发送id,id在jQuery端发送正常,但在http post请求完成后,我的表行仍然存在,有人能看到我的代码是否有问题,提前谢谢 <?php $sql_categories = "SELECT art_id, art_title, art_company, art_cat_id, art_sta_id, art_date, art_r
<?php
$sql_categories = "SELECT art_id, art_title, art_company, art_cat_id, art_sta_id, art_date, art_rating, art_price, cat_id, cat_name, sta_id, sta_name
FROM app_articles LEFT JOIN app_categories
ON app_articles.art_cat_id = app_categories.cat_id
LEFT JOIN app_status
ON app_articles.art_sta_id = app_status.sta_id
ORDER BY art_order ASC";
if($result = query($sql_categories)){
$list = array();
while($data = mysqli_fetch_assoc($result)){
array_push($list, $data);
}
foreach($list as $i => $row){
?>
<div class="row" id="page_<?php echo $row['art_id']; ?>">
<div class="column one">
<span class="icon-small move"></span>
<a href="edit-article.php?art_id=<?php echo $row['art_id']; ?>"><span class="icon-small edit"></span></a>
<span id="<?php echo $row['art_id']; ?>" class="icon-small trash"></span>
</div>
<div class="column two"><p><?php echo $row['art_title']; ?></p></div>
<div class="column two"><p><?php echo $row['art_company']; ?></p></div>
<div class="column two"><p><?php echo $row['cat_name']; ?></p></div>
<div class="column one"><p><?php echo $row['art_date']; ?></p></div>
<div class="column one"><p><?php echo $row['art_rating']; ?></p></div>
<div class="column one"><p><?php echo $row['art_price']; ?></p></div>
<div class="column one"><p><?php echo $row['sta_name']; ?></p></div>
<div class="column one"><a href=""><span class="icon-small star"></span></a></div>
</div>
<?php
}
}
else {
echo "FAIL";
}
?>
jQuery
$(document).ready(function(){
$(".trash").click(function(){
var del_id = $(this).attr('id');
$.ajax({
type:'POST',
url:'ajax-delete.php',
data: 'delete_id='+del_id,
success:function(data) {
if(data) {
}
else {
}
}
});
});
});
PHP mySQL Statement
if(isset($_POST['delete_id'])) {
$sql_articles = "DELETE FROM app_articles WHERE art_id = ".$_POST['delete_id'];
if(query($sql_articles)) {
echo "YES";
}
else {
echo "NO";
}
}
else {
echo "FAIL";
}
?>
jQuery
$(文档).ready(函数(){
$(“.trash”)。单击(函数(){
var del_id=$(this.attr('id');
$.ajax({
类型:'POST',
url:'ajax-delete.php',
数据:“删除id=”+删除id,
成功:功能(数据){
如果(数据){
}
否则{
}
}
});
});
});
PHP mySQL语句
如果(isset($\u POST['delete\u id'])){
$sql_articles=“从应用程序_文章中删除,其中art_id=“.$\u POST['DELETE_id'”;
if(查询($sql\U文章)){
回应“是”;
}
否则{
回应“否”;
}
}
否则{
回应“失败”;
}
?>
您的行仍然存在的原因是AJAX调用不会刷新页面。如果要删除行,必须执行以下操作:
假设span click事件位于行内
$(".trash").click(function(){
var del_id = $(this).attr('id');
var rowElement = $(this).parent().parent(); //grab the row
$.ajax({
type:'POST',
url:'ajax-delete.php',
data: {delete_id : del_id},
success:function(data) {
if(data == "YES") {
rowElement.fadeOut(500).remove();
}
else {
}
}
});
替换:
data: 'delete_id='+del_id,
与:
@谢谢GGio,但我的问题是从实际数据库中删除它,因为我正确地发送了我需要的数据(jQueryAjax),并且在sql页面中正确地接收到了它?因为它不会将其从数据库中删除。非常感谢这是我混淆的部分。非常感谢您的时间!非常感谢!
data: delete_id : del_id,