0

I want to delete a record in CodeIgniter but it's getting an error.

This is the model I created:

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

Controller:

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

And 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');
            }
        });
    }
}

Why can't it delete the record?

1
  • Its undefined $tipe in model Commented Feb 28, 2016 at 13:48

1 Answer 1

1

In model variable $tipe is undefined.

If it just a typo error than just remove from:

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

Should be:

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

If $tipe is your property than use like $this->tipe.

If you want to pass $tipe in ajax request than use like that:

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

Pass tipe in ajax data as you are using id

You can get both values in your controller by using $_POST like:

Your controller:

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

Your model:

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); 
}
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for your answer but I want to define $tipe in ajax url: "<?php echo site_url('paket/ajax_delete') ?>/" + id how to add tipe in ajax url?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.