我有幾行數據在運行時使用數據庫數據填充。每一行都有一個按鈕。點擊按鈕我想從數據庫中刪除該行。我在該按鈕上放了一個data-id
。點擊按鈕發送一個ajax請求到php。但我在php中得到null
id。以下代碼我使用。
HTMLcodeigniter input-> post()從jquery ajax發佈請求爲空
<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控制檯打印的用戶ID。 但在Ajax響應控制檯打印
Object {is_error: true, message: null}
什麼,我做錯了什麼?對不起,我英文很差。 TIA
你在AJAX使用'delete'方法,但你的方法名是'delete_client'。調整所有這些東西,然後檢查我的回答 –
沒有具體原因,我從'route.php'的'delete'路由到'delete_client'。 – ARiF