【问题标题】:How to fix confirm delete not working in codeigniter, ajax?如何修复确认删除在codeigniter,ajax中不起作用?
【发布时间】:2022-06-21 20:11:34
【问题描述】:

我错过了什么? 在“你确定要删除”中的“确定”之后,我什么也没得到。但是当我尝试提醒 $id 它工作。是ajax的问题​​吗?

我的 footer.php 中有这个

<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx" crossorigin="anonymous"></script>

<script>

    $(document).ready(function () {

        $('.confirm-delete').click(function (e) {
            e.preventDefault();

            confirmDialog = confirm("Are you sure you want to delete?");
            if(confirmDialog)
            {
                var id = $(this).val();
                // alert(id);
                $.ajax({
                    type: "DELETE",
                    url: "/employee/confirmdelete/"+id,
                    success: function (response) {
                        alert("Data deleted successfuly");
                       
                    }
                });
            }

        });
    });

</script>

我的 routes.php 中有这个

$route['employee/confirmdelete/(:any)']['DELETE'] = 'Frontend/EmployeeController/delete/$1';

我的删除按钮

<button type="button" class="btn btn-danger confirm-delete" value="<?= $row->id; ?> ">Confirm Delete</button>

我在控制器中的删除功能

public function delete($id)
{
    $this->load->model('EmployeeModel');
    $this->EmployeeModel->deleteEmployee($id);  
    redirect(base_url('employee'));
}

我的模型删除功能

public function deleteEmployee($id)
{
    return $this->db->delete('employee', ['id' => $id]);
}"

【问题讨论】:

    标签: json ajax codeigniter codeigniter-3 sql-delete


    【解决方案1】:

    你应该改变这个

    $.ajax({
                        type: "DELETE",
                        url: "/employee/confirmdelete/"+id,
                        success: function (response) {
                            alert("Data deleted successfuly");
                           
                        }
                    });
    

    到这里

    $.ajax({                    
                        url: "/employee/confirmdelete/"+id,
                        type: "POST",
                        dataType: "JSON"
                        success: function (response) {
                            alert("Data deleted successfuly");
                           
                        }
                    });
    

    【讨论】:

      猜你喜欢
      • 2021-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-12
      • 2023-03-13
      • 2021-04-11
      • 1970-01-01
      • 2014-08-12
      相关资源
      最近更新 更多