Confirmation on click update in CGrid

Confirmation on click update in CGrid

I swam in the code for 3 hours before. This is a way I found to add a javascript confirm box to a CGrid customized update button.

1- Add the yii app action you want to call and the confirmation javascript function at the beginning of the view with the CGrid that you want to customize


#myview with special confirm update CGrid

//Initialiser l'action du controleur... change this to your controller action

$actionURL = Yii::app()->createUrl("inscriptions/register");

//Ajouter le script pour traiter l'action en AJAX  ... idcontexte is the ID from de CGrid column... change this to whatever you need  

Yii::app()->clientScript->registerScript('confirmation', "        

    function confirmation(idcontexte){

        var choix = false;

        choix = confirm('Cette opération réinitialisera tous vos tirs pour ce contexte\\nVoulez-vous vraiment continuer?');



                    type: \"POST\",

                    url: \"$actionURL \",

                    data: \"idcontexte=\"+idcontexte,

                    success:alert('Initialisation des tirs complété'),



                                            alert('You are offline!!\\n Please Check Your Network.');

                                        }else if(x.status==404){

                                            alert('Requested URL not found.');

                                        }else if(x.status==500){

                                            alert('Internel Server Error.');

                                        }else if(e=='parsererror'){

                                            alert('Error.\\nParsing JSON Request failed.');

                                        }else if(e=='timeout'){

                                            alert('Request Time out.');

                                        }else {

                                            alert('Unknow Error.\\n'+x.responseText);






            return false;




2- Add click event confirmation function with special parameter to the button in the grid where you want to confirm AND remove the url to disable the default action of the button.



                        'template' => '{view} {update} {delete} {register}',






                                        //'url'=>'Yii::app()->createUrl("inscriptions/register?idcontexte=$data->idcontexte")', ... this line can be remove





                                        'label'=>'Mettre à jour',
















All this works because of this parameter in the javascript function : [b]



This parameter get the first CGrid column value. (in my situation this is the ID) If you need to get second or third parameter you just have to change the child number.

Of course you need a controller with the action you want to call : [b]

$actionURL = Yii::app()->createUrl("inscriptions/register");


This parameter call the register action in the inscription controller. Change this to whatever you need to confirm before execute

If every things works, you will have a javascript confirmation box before the action and an alert box return message for success or error


Alert message

If you need more details you can write to me.

Good Look !

Email me