Please Help me regarding confirm box

I am using this below code for confirmation before deleting an item in my index page.

But i want my confirm box to be a beautiful one. Can u tell me what i have to do???

I don’t understand how to integrate jquery plugin here… Please if u have time then help me…

my controller name is LatestnewsController

my model name is latestnews

my method name is actionDelete

echo CHtml::link(‘Delete’, “#”, array(“submit”=>array(“delete”,“id”=>"$latestnews->id" ),“confirm”=>“Are you sure?”));

You may want to use jQuery Impromptu or some other plugin

I have downloaded the plugin… and in my project’s …/…/layout/main.php i have added the script path…

but i don’t know how to implement it?

If you see the source code generated by Yii for your first code, you’ll find something like this


$('body').on('click', '#yt0', function() {

  if(confirm('Are you sure?')) {

    jQuery.yii.submitForm(this,'someController/delete/5',{});

    return false;

  } else

    return false;

});

You should in fact change your PHP code to render just the link with some HTML class (because an id would mean there’s just one link of such on the page).

So your code should change from


echo CHtml::link('Delete', "#", array("submit"=>array("delete","id"=>"$latestnews->id" ), "confirm"=>"Are you sure?"));

to


echo CHtml::link('Delete', array("delete","id"=>"$latestnews->id") , array('class'=>'someClass'));?>

and you add the needed JavaScript code for the class someClass like the one generated by Yii:


$('body').on('click', '.someClass', function(e){

  e.preventDefault();

  var txt = 'Are you sure?<input type=\"hidden\" name=\"href\" value=\"'+ $(this).attr('href') +'\" />';

  $.prompt(txt, {//look in the plugin doc and demos for more options

    buttons: {Delete:true, Cancel:false},

    callback: function(e,v,m,f) {

      if(v){

        jQuery.yii.submitForm(this, f.href, {});

        return false;

      } else

        return false;

    }

  });

});

Thanks for your reply…

but its not deleting…

Well it was an example.

What’s the JavaScript code generated by Yii for your first confirm CHtml link?

That’s the code you should put in the snippet, maybe with some adaptation.

You may need to include yii core script for using jQuery.yii.submitForm

you can do this by including the following Yii::app()->clientScript->registerCoreScript(‘jquery.yii’);

you can do something like this:

CHtml::link(

'Delete',


'#',


 array('submit'=&gt;array('wsrecruiteducation/delete','id'=&gt;&#036;model-&gt;EducID),


       'params'=&gt;('returnUrl'=&gt;'controller/action...'), 'confirm' =&gt; '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’]))

 &#036;this-&gt;redirect(isset(&#036;_POST['returnUrl']) ? array(&#036;_POST['returnUrl']) : array('admin'));