top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to delete a record using POST method in CakePHP?

0 votes
387 views
How to delete a record using POST method in CakePHP?
posted Jul 31, 2014 by Mohit Sharma

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

You can use postLink method of FormHelper .

In view file ,you should use:

echo $this->Form->postLink('Delete',
array('action' => 'delete', $list['Memberlist']['id']),
array('class'=>'btn-mini btn', 'confirm' => 'Are you sure?'));

In the Controller,use following code:
public function delete($id) {
if($this->request->is('get')) {
throw new MethodNotAllowedException();
}

$this->Memberlist->id = $id;
if (!$this->Memberlist->exists()) {
    throw new NotFoundException(__('Invalid list.'));
}
if ($this->Memberlist->delete()) {
    $this->Session->setFlash(__('List deleted.'), 'success');
    return $this->redirect(array('action'=>'index'));
}
$this->Session->setFlash(__('List was not deleted.'), 'error');
return $this->redirect(array('action'=>'index'));

}

answer Aug 1, 2014 by Rahul Mahajan
...