简体   繁体   中英

how to put confirm alert in controller and redirect to perticular link if cancel it in yii

i want to put confirm alert in controller and redirect to perticular link if cancel button clicked. My admin.php code is:

         array(
            'header' => 'Action',
            'class' => 'CButtonColumn',
            'template' => '{update}',
            'updateButtonUrl' => 'Yii::app()->createUrl("attendance/inout", array("id"=>$data->supplier_master_id))'
         )

Here i have to put confirm alert in controller because some extra condition also put in it.

My controller code is:

public function actionInout($id) { 
?>
<script> 
if(confirm("Do you want to add record?")) {
  // means proceed
} else {         
  // Should be redirect back to the grid view
} 
</script>
<?php // further code

You can open confirmation dialog on button click, please change your button code to:

array(
    'header' => 'Action',
    'class' => 'CButtonColumn',
    'template' => '{update}',
    'buttons' => array(
        'update' => array(
            'url' => 'Yii::app()->createUrl("attendance/inout", array("id"=>$data->supplier_master_id))',
            'click' => 'js:function(){if(!confirm("Do you want to add record?")) {return false;}}'
        )
    )
)

and remove script from your controller

I found answer by myself

as i have to put confirm alert in controller i did in controller like:

   public function actionInout($id,$final_status) { ?>
   <script>
       if(!confirm("Do you want to check- <?php echo $final_status; ?> to well sites.")) {
            window.location.href = url; // back url
       } else {
            window.location.href = url1; // further process
       }
 </script>
 <?php // code

You can't do this in this way. Controller works on data, but user interactions goes through view. In this meaning the right way is to implement same logic in your view. By this logic you'll activate from controller the appearing of alert question. After that along your answer you'll take the action again from controller. I've tried to illustrate this by list the actions:

1. Controller makes initial tasks and calls view
2. User enters what needed and finish with view
3. Controller analyze input data and initialize alert event for view - again calls view. Of course in call controller keeps and sends to view already filled data.
4. View understood that it called with alert action and shows the alert
5. After user action the view sends all data ( already containing and answer ) to the controller.
6. Controller understands alert and goes where needed.

I hope I've understood your question and my answer will give you a sense.

Of course it can be a solution, where all alert/answer logic is made by javascript into the view, but it is very dependent of concrete case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM