Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ajax删除按钮从数据库中删除,但我必须重新加载页面才能看到更改?_Ajax - Fatal编程技术网

ajax删除按钮从数据库中删除,但我必须重新加载页面才能看到更改?

ajax删除按钮从数据库中删除,但我必须重新加载页面才能看到更改?,ajax,Ajax,第一页 <script> $(document).ready(function(){ $(".delete_buttom").click(function(){ var x = $(this).attr('id'); click_delete(x); }); function click_delete(x){ var commentId = x; $.post("ajax_co

第一页

<script>
    $(document).ready(function(){         
    $(".delete_buttom").click(function(){  
    var x = $(this).attr('id');  
    click_delete(x);  
    });  

    function click_delete(x){  
    var commentId = x;  
    $.post("ajax_comments3.php",  
   {  
        task : "this is the task",  
        commentId : commentId  
    }  
    ).success(  
        function(data){  
             $('.li_style').remove(data);  
        }
    ).error(function(){  
       alert("404 not found");  
        });  
}  
</script>  
<ul class="ul_style">                  
<?php while($row = mysqli_fetch_array($excute_select)){  

if(!empty($row['commet_text'])) {  ?>  

    <li class="li_style" >  

   <img src="profile.jpg" class="user_img_src" />  

   <h5 class="username"><?php echo "mohamed daif" ;?></h5>  

   <div class="delete_buttom" id="<?php echo $row['comment_id'] ; ?>">X</div>  

   <div class="user_comment"><?php echo $row['commet_text'] ; ?> </div>  

   </li>  

  <?php }  

  }   ?>    

 </ul>    
<?php  
$host_name = "localhost";  
$database_user = "root";  
$password = "";  
$database_name = "comments";  
if(isset($_POST["task"]) && $_POST["task"] == "this is the task"){  
    $commentId = $_POST["commentId"];  
    $connection = mysqli_connect($host_name,$database_user,$password,$database_name) 
        or die("connection failed");  
   $delete_query = "delete from comment where comment_id = $commentId ";  
   $excute_delete = mysqli_query($connection,$delete_query) or die("delete query error");  
}  
?>  

$(文档).ready(函数(){
$(“.delete_buttom”)。单击(函数(){
var x=$(this.attr('id');
单击〖删除〗按钮(x);
});  
函数单击删除(x){
var=x;
$.post(“ajax\u comments3.php”,
{  
任务:“这就是任务”,
commentId:commentId
}  
).成功(
函数(数据){
$('.li_style')。删除(数据);
}
).error(函数(){
警报(“404未找到”);
});  
}  
html代码

<script>
    $(document).ready(function(){         
    $(".delete_buttom").click(function(){  
    var x = $(this).attr('id');  
    click_delete(x);  
    });  

    function click_delete(x){  
    var commentId = x;  
    $.post("ajax_comments3.php",  
   {  
        task : "this is the task",  
        commentId : commentId  
    }  
    ).success(  
        function(data){  
             $('.li_style').remove(data);  
        }
    ).error(function(){  
       alert("404 not found");  
        });  
}  
</script>  
<ul class="ul_style">                  
<?php while($row = mysqli_fetch_array($excute_select)){  

if(!empty($row['commet_text'])) {  ?>  

    <li class="li_style" >  

   <img src="profile.jpg" class="user_img_src" />  

   <h5 class="username"><?php echo "mohamed daif" ;?></h5>  

   <div class="delete_buttom" id="<?php echo $row['comment_id'] ; ?>">X</div>  

   <div class="user_comment"><?php echo $row['commet_text'] ; ?> </div>  

   </li>  

  <?php }  

  }   ?>    

 </ul>    
<?php  
$host_name = "localhost";  
$database_user = "root";  
$password = "";  
$database_name = "comments";  
if(isset($_POST["task"]) && $_POST["task"] == "this is the task"){  
    $commentId = $_POST["commentId"];  
    $connection = mysqli_connect($host_name,$database_user,$password,$database_name) 
        or die("connection failed");  
   $delete_query = "delete from comment where comment_id = $commentId ";  
   $excute_delete = mysqli_query($connection,$delete_query) or die("delete query error");  
}  
?>  
在html代码中,我使用php从数据库中获取数据 第二页

<script>
    $(document).ready(function(){         
    $(".delete_buttom").click(function(){  
    var x = $(this).attr('id');  
    click_delete(x);  
    });  

    function click_delete(x){  
    var commentId = x;  
    $.post("ajax_comments3.php",  
   {  
        task : "this is the task",  
        commentId : commentId  
    }  
    ).success(  
        function(data){  
             $('.li_style').remove(data);  
        }
    ).error(function(){  
       alert("404 not found");  
        });  
}  
</script>  
<ul class="ul_style">                  
<?php while($row = mysqli_fetch_array($excute_select)){  

if(!empty($row['commet_text'])) {  ?>  

    <li class="li_style" >  

   <img src="profile.jpg" class="user_img_src" />  

   <h5 class="username"><?php echo "mohamed daif" ;?></h5>  

   <div class="delete_buttom" id="<?php echo $row['comment_id'] ; ?>">X</div>  

   <div class="user_comment"><?php echo $row['commet_text'] ; ?> </div>  

   </li>  

  <?php }  

  }   ?>    

 </ul>    
<?php  
$host_name = "localhost";  
$database_user = "root";  
$password = "";  
$database_name = "comments";  
if(isset($_POST["task"]) && $_POST["task"] == "this is the task"){  
    $commentId = $_POST["commentId"];  
    $connection = mysqli_connect($host_name,$database_user,$password,$database_name) 
        or die("connection failed");  
   $delete_query = "delete from comment where comment_id = $commentId ";  
   $excute_delete = mysqli_query($connection,$delete_query) or die("delete query error");  
}  
?>  

它工作正常,可以从数据库中删除,但在浏览器中,当我单击“删除”按钮时,它会隐藏所有其他注释,直到我重新加载页面以查看更改


如果我的问题中有任何不清楚的地方,请告诉我。

在您的列表元素中添加一个id标记,如

<li id="comment_<?php echo $row['comment_id'] ; ?>" class="li_style" >  

您的代码的哪一部分应该更新页面,以及该更改应该是什么?在第二个页面中,它应该删除注释框,但在我自己重新加载页面之前,我看不到更改。我认为从DOM中删除元素比用CSS隐藏好。谢谢,这很好,但可以您解释了click_delete函数,因为您在传递注释id的click delete函数中让我感到困惑。之前,您接收变量x作为参数,并声明第二个具有相同值的变量commentId,因此我刚刚删除了该变量。然后,您正在进行ajax调用,并将注释id传递给您的用户服务器,以便它知道要删除哪个注释。只要服务器上没有发生错误,success函数就会执行。在这里,我只是获取具有正确id的列表元素并将其隐藏在页面上。但我希望删除它,而不是隐藏它,然后将.hide()更改为.remove()