ARiF ARiF - 5 months ago 64
Ajax Question

codeigniter input->post() is null from jquery ajax post request

I have several rows of data populated using database data at run time. Each row has a button. On click button i want to delete that row from database. I put a

data-id
on that button. On click the button send an ajax request to php. But i get
null
id in php. Following code i use.



HTML

<span id="delete_btn" class="btn btn-default" data-id="<?php echo $id; ?>">
<i class="glyph-icon icon-close"> Delete</i>
</span>


JavaScript

$('#feedbackTable').on('click', '#delete_btn', function (event) {

var client_id = $(this).data('id');


console.log(client_id); // I get the exact id
$.ajax({
url: url + 'client/delete',
type: 'POST',
data: {'id' : client_id},
dataType: 'json',
async: false,
cache: false,
contentType: false,
processData: false,
error: function (error_data) {
console.log(error_data);
},
success: function (data) {
console.log(data);
}
});
return false;
});


Client.php

public function delete_client() {
if ($this->input->method(TRUE) == 'POST') {
$id = $this->input->post('id');

if (is_numeric($id)) {
$id = intval($id);
echo json_encode(array(
"is_error" => false,
"message" => $id
));
return;
} else {
echo json_encode(array(
"is_error" => true,
"message" => $id
));
return;
}
} else {
echo json_encode(array(
"is_error" => true,
"message" => "Invalid request"
));
return;
}
}


javascript console prints an user id.
but in ajax response console prints

Object {is_error: true, message: null}


what i am doing wrong? Sorry for my poor English. TIA

Answer

Instead of using is_numeric() you may convert value by type casting. Try following

public function delete_client() {
    if ($this->input->method(TRUE) == 'POST') {
        $id = (int) $this->input->post('id');

        if (!empty($id)) {
            echo json_encode(array(
                "is_error" => false,
                "message" => $id
            ));
            return;
        } else {
            echo json_encode(array(
                "is_error" => true,
                "message" => $id
            ));
            return;
        }
    } else {
        echo json_encode(array(
            "is_error" => true,
            "message" => "Invalid request"
        ));
        return;
    }
}

Secondly remove unnecessary params from your ajax

$('#feedbackTable').on('click', '#delete_btn', function (event) {

     var client_id = $(this).data('id');


    console.log(client_id); // I get the exact id
    $.ajax({
        url: url + 'client/delete',
        type: 'POST',
        data: {'id' : client_id},
        dataType: 'json',
        error: function (error_data) {
           console.log(error_data);
        },
        success: function (data) {
            console.log(data);
        }
    });
    return false;
});

Following removed.

        async: false,
        cache: false,
        contentType: false,
        processData: false,

contentType, processData etc used when your form contain images etc etc. Not required here