Skip to content

Instantly share code, notes, and snippets.

@taleeb35
Created September 21, 2014 09:30
Show Gist options
  • Save taleeb35/babd6389232e85b76551 to your computer and use it in GitHub Desktop.
Save taleeb35/babd6389232e85b76551 to your computer and use it in GitHub Desktop.
Delete with Jquery
public function delete($id = null) {
if ($this->request->is('ajax')) {
$this->autoRender = false;
$this->layout = null;
$response = array();
$cur_date = date("Y-m-d H:i:s");
$this->request->data['User']['is_deleted']=1;
$this->request->data['User']['is_active']=0;
$this->request->data['User']['date_deleted']=$cur_date;
$this->request->data['User']['date_updated']=$cur_date;
$this->User->id = $id;
if ($this->User->save($this->request->data)) {
$response['success'] = true;
$response['message'] = __('User deleted',array('class' => 'message'));
$response['redirect'] = Router::url(array('action' => 'index'));
} else {
$response['success'] = false;
$response['message'] = __('User was not deleted');
$response['redirect'] = Router::url(array('action' => 'index'));
}
echo json_encode($response);
}
die();
}
$(document).ready(function() {
$('a.delete').click(function(e) {
// var __this = this;
e.preventDefault();
var parent = $(this).parent("td").parent("tr");
$.ajax({
type: 'get',
url: $(this).attr("href"),
dataType: 'json',
beforeSend: function() {
parent.animate({'backgroundColor':'#C3C3C3'},1000);
},
success: function(response) {
if(response.success){
alert("Are you sure to delete the user?");
parent.slideUp(1000,function() {
parent.remove();
});
}else{
alert("Failed to delete the user");
parent.animate({'backgroundColor':'#fff'},1000);//Restore your background back
}
}
});
});
});
<a href="<?php echo $this->Html->url(array("action" => "delete", $user['User']['id'])) ?>" class="delete">Delete</a></td>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment