Codeigniter - 删除而不刷新页面

时间:2015-01-09 08:02:55

标签: javascript ajax codeigniter

我在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甚至没有被删除。有答案吗?

2 个答案:

答案 0 :(得分:0)

两位认为您需要更改代码

首先在模型中返回restult

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>

答案 1 :(得分:0)

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

如果您完全按照问题中的说明编写了代码,我认为您的ajax请求甚至没有被触发,因为您正在调用$('.delete').click(...,而在您的代码中,您已将链接ID设置为&#39 ;删除&#39;不上课!

在删除按钮中添加类删除,一切都应该正常..:)

<强>更新 或者你可以打电话给$('#delete').click(...

更新2:

您的代码存在一些问题(我会在更改时评论问题)

控制器功能:

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");
         }
       }
   });
});
});