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

Delete record with out page refresh in codeigniter is not working

大家好我正在尝试从数据表中删除我的记录,而我已经使用了 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));

}

任何人都可以帮助我如何在不刷新页面的情况下做到这一点我想删除我的记录。

提前致谢。

试试这个:

$(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 onClick:

<button type="submit" class="btn btn-danger btn-xs confirmation empdelete" id="<?php echo $emp->id;?>"><i class='fas fa-times'></i></button>

希望对您有所帮助:

你的按钮应该是这样的:

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