1

View file

<button class="btn btn-danger deleteBlog" onclick="deleteBlog(<?php echo $row->id;?>)"></button>

Ajax part

function deleteBlog(id) {
    var base_url = "<?php echo base_url()?>";
    if (confirm("Are you sure?")) {
        $.ajax({
            url: base_url + 'blog/delete',
            type: 'post',
            data: {id: id},
            success: function () {
                alert('ajax success');
            },
            error: function () {
                alert('ajax failure');
            }
        });
    } else {
        alert(id + " not deleted");
    }
} 

controller file

function delete(){
    $id = $this->input->post('id');
    $this->_delete($id);
}

function _delete($id)
{
    $this->load->model('mdl_blog');
    $this->mdl_blog->_delete($id);
}

model

function _delete($id)
{
    $table = $this->get_table();
    $this->db->where('id', $id);
    $this->db->delete($data);
}

Hello Guyz I am bit confused with this ajax deleting concept I might be doing some silly mistake or I am totally out of the concept of ajax. Any help and suggestion will be appreciated. thanks

JMS786
  • 1,103
  • 2
  • 11
  • 22
metalhead101
  • 111
  • 3
  • 18

2 Answers2

0

Try to delete this code:

$table = $this->get_table();

You don't need this line in CI model delete function..

And in controller:

function delete(){
    $id = $this->input->post('id');
    $this->_delete($id);
    echo "1";
}
JMS786
  • 1,103
  • 2
  • 11
  • 22
0

Try to see the query with $this->db->last_query(); to check if current query to delete it's ok.

See: https://www.codeigniter.com/user_guide/database/helpers.html

p1errot
  • 41
  • 1
  • 4