I am trying to delete a list of entries form my table corresponding to a particular id. I get the id and post it through an ajax function to the controller and use the Delete function of the model to delete the particular entry. But the entries are not deleted.
This is my ajax function in the view file called reports.ctp where I call the controller function when a link is clicked.
$(".delete_entries").click(function() {
$.ajax({
type: "POST",
url: "http://localhost/FormBuilder/reports/deleteEntries",
data: "formid="+formid,
async: false,
success: function(msg){
alert( "Data Saved: " + msg);
}
});//ajax
});
This is the delete action in the reports_controller.php
function deleteEntries()
{
$this->data['Result']['form_id']=$this->params['form']['formid'];
$this->Report->Result->delete($this->data['Result']['form_id']);
}
The table from which I want to delete the entries is 'Results'. some one help me as to how to delete the entries.
EDIT
Now I'm using the delete sql query as such to delete the entries in the Results table.
$this->Result->query("delete from results where form_id=".$this->data['Result']['form_id']);
I do not know why the delete command of CakePHP doesn't work.
But now the problem is, only when I refresh the page, the deletion of the entries are reflected. If I do not refresh, the entries are still displayed in the table. Maybe if the delete function of CakePHP works, the page will get refreshed.
SOLUTION
The deleteAll method works since I'm not giving the primary id as the input for the delete method as deceze has pointed out.
$this->Result->deleteAll(array('Result.form_id' => $this->data['Result']['form_id']));
Regarding the problem of reflecting the deletion of entries, I did a $(".entries).remove() on success as Xr had suggested, so the table of entries is removed without having to do a page refresh.