How to create a link with confirmation dialog using Yii?

23,580

Solution 1

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?')
);

Solution 2

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'));
...
Share:
23,580

Related videos on Youtube

sasori
Author by

sasori

code monkey

Updated on February 05, 2020

Comments

  • sasori
    sasori about 4 years

    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?

    • sasori
      sasori about 13 years
      solved it by CHtml::link("Delete","#",array("submit"=>array("wsrecruitedu‌​cation/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 ?

Related