17

How can I create a link with a confirmation dialog in Yii framework?

Let's say I have

CHtml::link('Delete',array('wsrecruiteducation/delete','id'=>$model->EducID));

how do I convert that code snippet above, into a delete link with a confirm alert before deleting the data?

Charles
  • 50,943
  • 13
  • 104
  • 142
sasori
  • 5,249
  • 16
  • 86
  • 138
  • 1
    solved it by `CHtml::link("Delete","#",array("submit"=>array("wsrecruiteducation/delete","id"=>"$val->EducationID"),"confirm"=>"are you sure?"))` but then how to redirect the user to the same page after confirming he deletion of the item ? – sasori Mar 09 '11 at 13:37
  • Any idea for using trash icon? – ersks Oct 27 '16 at 03:34

3 Answers3

30

You just need to also use the last parameter of CHtml::link:

CHtml::link(
    'Delete',
     array('wsrecruiteducation/delete','id'=>$model->EducID),
     array('confirm' => 'Are you sure?')
);
Jon
  • 428,835
  • 81
  • 738
  • 806
  • solved it by CHtml::link("Delete","#",array("submit"=>array("wsrecruiteducation/delete","id"­=>"$val->EducationID"),"confirm"=>"are you sure?")) but then how to redirect the user to the same page after confirming he deletion of the item ? – sasori Mar 09 '11 at 13:42
  • @sasori: By calling `redirect` from your controller after it deletes the data. See http://www.yiiframework.com/doc/api/1.1/CController#redirect-detail. You could also turn the whole thing into an AJAX request for a more seamless user experience. – Jon Mar 09 '11 at 13:45
  • i know what redirect is, and i've been using that in some areas of my app, but my question is, how to use redirect() within that CHtml::link, after confirming the deletion ? :S – sasori Mar 09 '11 at 13:47
  • @sasori: after confirming the deletion the browser will navigate to `/index.php/wsrecruiteducation/delete/` where the deletion will actually be done. You will redirect from there. I don't quite get the question. – Jon Mar 09 '11 at 13:49
  • am at this page `wsrecruitcvhead/educationqualification&id=1` that is the page where the delete link can be found, if the delete link is calling this `wsrecruiteducation/delete` ,how will I redirect back to the `wsrecruitcvhead/educationqualification&id=1` page ? how to use the redirect inside the CHtml::link() after confirmation of delete ? – sasori Mar 09 '11 at 13:53
  • 1
    @sasori: You cannot redirect from inside the link of course. The code in `wsrecruiteducation/delete` needs to run first to delete the data. At that point *there is no link anymore*; the browser has already navigated to another page. Look into using AJAX if the redirect is not satisfactory. – Jon Mar 09 '11 at 13:56
  • @sasori, you are confusing client and server execution. i would recommend reading up on that. also, you are breaking your application if the user does not use javascript. please read up on progressive enhancement – Neil McGuigan Mar 09 '11 at 17:45
  • In Yii.,...by default...you wont be able to delete a record through this approach since this would be deleting through GET request...to have it to work you will have to edit your delete method to accept GET request...and this is somehow dangerous as web crawler call links directly...which might end up cleaning up your database...use it wisely – Eliethesaiyan Oct 31 '13 at 09:51
  • actually...this would work fine as it enforce a POST request... CHtml::link("Delete","#",array("submit"=>array("wsrecruiteducation/delete","id"‌​=>"$val->EducationID"),"confirm"=>"are you sure?"))..credit to the person who asked the question! – Eliethesaiyan Oct 31 '13 at 09:53
4

you can do something like this:

CHtml::link(
    'Delete',
    '#',
     array('submit'=>array('wsrecruiteducation/delete','id'=>$model->EducID),
           'params'=>('returnUrl'=>'controller/action...'), 'confirm' => 'Are you sure?')
);

The returnUrl will be a post item sent with the request, make sure you make something like this in a controller with delete action:

...
if(!isset($_GET['ajax']))
     $this->redirect(isset($_POST['returnUrl']) ? array($_POST['returnUrl']) : array('admin'));
...
Helidium
  • 41
  • 1
0

If you wan't a delet Link with confirmation Dialog, use this

echo CHtml::link("Delete", '#', array(
'submit'=>array('controller/delete', "id"=>$model->id), 'confirm' => 'Are you sure you want to delete?'));
kasoft
  • 456
  • 6
  • 19