代码之家  ›  专栏  ›  技术社区  ›  suresh

在codeigniter中删除没有页面刷新的记录不工作

  •  2
  • suresh  · 技术社区  · 6 年前

    嗨,我正试图从数据表中删除我的记录,在codeigniter中没有页面刷新我使用了ajax,我不知道我在哪里做了错误的,没有删除记录

    以下是我的观点:

        <tbody>
                            <?php
                            if (!empty($employees)) {
                                foreach ($employees as $emp) {
                                     ?>
    
                                    <tr>
                                        <td><?php echo $emp->emp_name; ?></td>
                                        <td><?php echo $emp->salary; ?></td>
                                        <td class='text-center'>
                                        <button type="submit" class="btn btn-info btn-xs confirmation" name="login"><i class='fas fa-edit'></i></button>
                                        </td>
    
                                        <td class='text-center'>
                                         <button type="submit"  onClick="return ConfirmDelete()" class="btn btn-danger btn-xs confirmation empdelete" id="<?php echo $emp->id;?>"><i class='fas fa-times'></i></button>
                                         </td>
                                   </tr>
    
                                    <?php
                                }
                            }
                            ?>
                        </tbody>
        <script>
    $(document).ready(function(){
    $(".empdelete").click(function(e){
        alert();
       e.preventDefault(); 
        $.ajax({
          alert();
          type: "POST",
          url: "<?=site_url('Employee/delete');?>",
          cache: false,
          data: {id:$(this).attr("id")}, // since, you need to delete post of particular id
          success: function(data) {
             if (data){
                alert("Success");
             } else {
                 alert("ERROR");
             }
             return false;
    
           }
       });
    });
    });
    </script>
    

    这是我的控制器:

      function delete()  
      {
        $id = $this->input->post('id'); // get the post data
        $empdelete=$this->Emp_model->delete($id);
        if($empdelete){
            echo true;
        } else {
            echo false;
        }
     }
    

    以下是我的模型的方法删除:

    function delete($id)
    { 
    $sql  = "DELETE FROM employees WHERE id=?";
    return $this->db->query($sql,array($id));
    

    }

    有谁能帮我在没有页面刷新的情况下怎么做我想删除我的记录。

    提前谢谢。

    2 回复  |  直到 6 年前
        1
  •  1
  •   Alex    6 年前

    试试这个:

    $(document).ready(function () {
            function ConfirmDelete() {
                var x = confirm("Are you sure you want to delete?");
                if (x)
                    return true;
                else
                    return false;
            }
            $(".empdelete").click(function (e) {
                var obj = $(this);
                e.preventDefault();
                //alert(); what's this do?
                if (ConfirmDelete() == false) {
                    return false;
                }
                $.ajax({
                    //alert(); this can't go here
                    type: "POST",
                    url: "<?php echo site_url('Employee/delete'); ?>",
                    cache: false,
                    data: {id: $(this).attr("id")},
                    success: function (data) {
                        console.log('ajax returned: ');
                        console.log(data);
                        if (data) {
                            obj.closest('tr').remove();
                            alert("Success");
                        } else {
                            alert("ERROR");
                        }
                        return false;
                    }
                });
            });
        });
    

    然后单击删除HTML:

    <button type="submit" class="btn btn-danger btn-xs confirmation empdelete" id="<?php echo $emp->id;?>"><i class='fas fa-times'></i></button>
    
        2
  •  1
  •   Pradeep    6 年前

    希望这对你有帮助:

    你的按钮应该是这样的:

    <button type="button"  onClick="return ConfirmDelete(this)" class="btn btn-danger btn-xs confirmation empdelete" data-id="<?=$emp->id;?>"><i class='fas fa-times'></i></button>
    

    您的ajax代码应该如下所示:

    function ConfirmDelete(obj) 
    { 
      var x = confirm("Are you sure you want to delete?"); 
      if (x == true) 
      {
        var id = $(obj).data('id');
        alert(id);
        if (id != '')
        {
          //do you ajax here
            $.ajax({
              type: "POST",
              url: "<php echo site_url('Employee/delete'); ?>",
              cache: false,
              data: {'id': id},
              success: function (data) {
                  console.log('ajax returned: ');
                  console.log(data);
                  if (data) {
                      alert("Success");
                  } else {
                      alert("ERROR");
                  }
                  return false;
              }
          });
        }
      }
      else
      {
       return false;
      }
    }
    

    你的控制器应该是这样的:

    function delete()  
    {
        $id = $this->input->post('id'); // get the post data
        $empdelete = $this->Emp_model->delete($id);
        if($empdelete)
        {
            echo true;
        } else 
        {
            echo false;
        }
        exit;
    }
    

    您的删除方法应如下:

    function delete($id)
    { 
        $this->db->where('id',$id);
        $this->db->delete('employees');
        return $this->db->affected_rows();
    }
    
    推荐文章