Codeigniter - 删除而不刷新页面

Codeigniter - Delete without refreshing page

我在 codeigniter 中 ajax 删除有问题。我想删除而不刷新页面

这是我的控制器

    function delete($id_post='')
    {
    $hapus=$this->home_model->delete($id_post);
                if($hapus){
                echo "true";
                } else {
                echo "error";
      }
    }

这是我的模型:

function delete($id_post='')
    { 
    $sql  = "DELETE FROM post WHERE id_post=?";
    return $this->db->query($sql,array($id_post));
}

这是我的观点和 javascript:

<?php foreach($news as $data):?>
 <div id="row">
<?php echo $data['news']?>
Post by: <?php echo ['username']?>
<button onClick="return confirm('Sure to delete?')" class="btn-danger btn-xs delete" id="<?php echo $data['id_post']?>">delete</button>
</div>
<?endforeach;?>

    <script languge="javascript" type="text/javascript">
   $(document).ready(function(){ // added
    $(".delete").click(function(){
        //var id_post = this.id;
        var btn = this;
        e.preventDefault();
            $.ajax({
               type: "POST",
               url: "<?php echo base_url()?>delete",
               cache: false,
               data: "id_post="+$(this),
               success: function(reaksi){
                   if (reaksi=="true"){
                       alert('Success delete');
                   } else {
                       alert('failed');
                   }
                }
            });
    return false
    });
   }); // added
</script>

当我点击删除时,ajax甚至没有触发。有答案吗?

两个人认为您需要更改代码

首先在您的模型中 return 结果

function delete($id_post='')
{ 
    $sql  = "DELETE FROM post WHERE id_post=?";
    return $this->db->query($sql,array($id_post));
}

其次在 ajax 函数中添加 return false

<script type='text/javascript'>
   $(document).ready(function(){
     $("#delete").click(function(e){
       //alert("Delete?");
         e.preventDefault(); 
         var href = $(this).attr("href");
         var btn = this;

        $.ajax({
          type: "POST",
          url: href,
          success: function(reaksi) {
             if (reaksi=="Success"){
                alert('Success')
             } else {
                 alert("ERROR");
             }
             return false;

           }
       }
    });
   return false;

   })
  });
</script>

我试着把它写成评论,但因为我是新来的所以我没有所需的声誉。

如果您编写的代码与您在问题中发布的代码完全相同,我认为您的 ajax 请求甚至没有被触发,因为您正在调用 $('.delete').click(... 而在您的代码中您输入了 id link 作为 'delete' 而不是 class!

在删除按钮中添加 class 删除,一切正常..:)

更新: 或者您可以改为调用 $('#delete').click(...

更新二:

你的代码有几个问题(我会在更改时评论问题)

控制器功能:

function delete() // you arent supplying any arguments in url of ajax call 
{
    $id_post = $this->input->post('id_post'); // get the post data
    $hapus=$this->home_model->delete($id_post);
    if($hapus){
        echo true;
    } else {
        echo false;
    }
}

Javascript:

 $(document).ready(function(){
 $(".delete").click(function(e){
    e.preventDefault(); 
    $.ajax({
      type: "POST",
      url: "<?php echo base_url()?>delete",
      cache: false,
      data: {id_post:$(this).attr("id")}, // since, you need to delete post of particular id
      success: function(reaksi) {
         if (reaksi){
            alert("Success");
         } else {
             alert("ERROR");
         }
       }
   });
});
});