问题
I need some help in my issue. I have a list of users and I want to delete user onclick of delete button using ajax in CI HMVC. Here is code of my list view
$(function() {
$(".tip-del").click(function() {
var recId = $(this).data("id");
var parent = $(this).parent();
var BASE_URL = "<?php echo base_url()?>";
var r = confirm("Are you sure you want to delete this user?");
if(r == true){
$.ajax({
url : BASE_URL + 'auth/delete_user',
type: 'post',
data: {'rec_id' : recId },
success: function (response){
try{
if(response == 'true'){
parent.slideUp('slow',function(){$(this).remove();});
}
}catch(e) {
alert('Exception while request..');
}
},
error: function (xhr, text, message) {
console.log(message);
}
});
return false;
}
});
});
And here is my controller(same module where the view is located) code
function delete_user() {
if ($this->input->post('rec_id')) {
$id = $this->input->post('id');
}
if (!$this->ion_auth->in_group('admin')) {
$this->session->set_flashdata('message', $this->lang->line("access_denied"));
$data['message'] = (validation_errors() ? validation_errors() : $this->session->flashdata('message'));
redirect('module=auth&view=users', 'refresh');
}
$this->ion_auth_model->deleteUser($id);
}
And here is my model code
public function deleteUser($id) {
if ($this->db->delete('users', array('id' => $id)) && $this->db->delete('users_groups', array('user_id' => $id))) {
return true;
}
return FALSE;
}
Can anybody please help me for this. I cant understand what I am going wrong. In a preview of ajax response I got the error as
An Error Was Encountered. The action you have requested is not allowed.
Thanks in advance.
回答1:
I think your delete query is wrong. It should be like below (in model code):
if ($this->db->where(array('id' => $id))->delete('users') && $this->db->where(array('user_id' => $id))->delete('users_groups')) {
return true;
}
来源:https://stackoverflow.com/questions/26228533/codeigniter-hmvc-ajax