无法使用 AJAX 删除 CodeIgniter 中的记录



我想删除CodeIgniter中的记录,但它收到错误。

这是我创建的模型:

public function delete_by_id($id) {
    $My_Multiple_Statements_Array = array('id' => $id, 'tipe' => $tipe);
    $this->db->where($My_Multiple_Statements_Array);
    $this->db->delete($this->table);
}

控制器:

public function ajax_delete($id) {
    $this->paket->delete_by_id($id);
    echo json_encode(array("status" => TRUE));
}

和 AJAX:

function delete_person(id)
{
    if (confirm('Are you sure delete this data?'))
    {
        // ajax delete data to database
        $.ajax({
            url: "<?php echo site_url('paket/ajax_delete') ?>/" + id ,
            type: "POST",
            dataType: "JSON",
            success: function (data)
            {
                //if success reload ajax table
                $('#modal_form').modal('hide');
                reload_table();
            },
            error: function (jqXHR, textStatus, errorThrown)
            {
                alert('Error deleting data');
            }
        });
    }
}

为什么它不能删除记录?

在模型中,变量$tipe未定义。

如果只是一个拼写错误,而不仅仅是从以下位置删除:

$My_Multiple_Statements_Array = array('id' => $id, 'tipe' => $tipe);

应该是:

$My_Multiple_Statements_Array = array('id' => $id);

如果$tipe是你的财产,那么像$this->tipe一样使用。

如果你想在ajax请求中传递$tipe,而不是像这样使用:

$.ajax({ 
url: "<?php echo site_url('paket/ajax_delete') ?>/", 
type: "POST", 
data: "id="+id+"&tipe="+tipe,
dataType: "JSON", 
success: function (data) { 
//if success reload ajax table $('#modal_form').modal('hide'); reload_table(); 
},

在使用 ajax 数据时传递tipe id

您可以使用如下$_POST在控制器中获取这两个值:

您的控制器:

public function ajax_delete() { 
    $id = $_POST["id"];
    $tipe = $_POST["tipe"];
    $this->paket->delete_by_id($id,$tipe); 
    echo json_encode(array("status" => TRUE)); 
}

您的型号:

public function delete_by_id($id,$tipe) { 
    $My_Multiple_Statements_Array = array('id' => $id, 'tipe' => $tipe); 
    $this->db->where($My_Multiple_Statements_Array); 
    $this->db->delete($this->table); 
}

最新更新