0

I am working on a project, where inside admin panel I have a table for enquiries and I want to allow admin to delete any of them. My view code is as follows:

<button type="button" class="btn btn-danger delbtn" onclick="delenquiry(<?php echo $value->id;?>)">Delete this Enquiry</button>

My ajax code is:

function delenquiry(id) {
    if (confirm("Are you sure?")) {
        $.ajax({
            url: base_url + 'loginc/delenq',
            type: 'post',
            data: id,
            success: function () {
                alert('ajax success');
            },
            error: function () {
                alert('ajax failure');
            }
        });
    } else {
        alert(id + " not deleted");
    }
}

Controller code is:

 public function delenq() {

        $id = $this->input->post('id');
        $this->logins->delenqs($id);
    }

And model code is:

public function delenqs($id) {
    $this->db->where('id', $id);
    $this->db->delete('enquiry');

}

I looked for answers, but didn't got any. Can anyone tell me what's wrong with my code. Thanks in advance...

4
  • are you getting any errors? any error message in the console? Commented Aug 26, 2015 at 8:37
  • @mdamia no, i am getting and alert saying "ajax success" Commented Aug 26, 2015 at 8:42
  • var_dump($id); or post data? are you getting the id? Commented Aug 26, 2015 at 8:43
  • you have ajax syntax error. data : { id:id } Commented Aug 26, 2015 at 8:46

2 Answers 2

4

You need to pass id from your ajax request change

 data: id,

To

 data: {id:id},

for data you must provide an array . for example => data:{name_params:10} you can get data in php $id = $this->input->post('name_params'); and the value $id will be = 10

Sign up to request clarification or add additional context in comments.

1 Comment

thanks, it worked. Can you tell me what mistake did I make?
1

The issue you have is that your ID is not an available key in your POST. You would need to define {id : id}. However, I think this makes more sense from an MVC approach:

$.ajax({
    url: base_url + 'loginc/delenq/'+ id, //maintains the (controller/function/argument) logic in the MVC pattern
    type: 'post',
    success: function(data){
        console.log(data);
    },
    error: function(a,b,c){
        console.log(a,b,c);
    }
});

Then you can expect an argument to your controller function:

public function delenq($id){
    if($id)
        return $this->logins->delenqs($id);
    return false;
}

And finally, your model can get a little fixer upper so that it properly returns as well.

public function delenqs($id) {
    $this->db->delete('enquiry', array('id' => $id));
    return $this->db->affected_rows() > 1 ? true:false;

}

Comments

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.