在代码点火器中删除没有页面刷新的记录不起作用



嗨,我正在尝试从数据表中删除我的记录,而无需在代码点火器中刷新页面,我使用了 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:

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

相关内容

  • 没有找到相关文章

最新更新