简体   繁体   中英

Render action based on confirmation in Yii

I have a question. Is there a way to render other action based on the confirmation message? For example I have this:

 <?php $this->widget('booster.widgets.TbButton', array(
        'buttonType'=>'submit',
        'context'=>'primary',
                        'htmlOptions'   => array(
                        'id'=> 'createstudent',
                        'confirm' => 'Do you want to register the student to a class'
                     ),
        'label'=>$model->isNewRecord ? Yii::t('default', 'Create') : Yii::t('default', 'Save'),
    )); ?>

And what I want to do is when the user click's Yes in the confirmation dialogue I want to render another action from another controller. For example contract/studentcreate, array($model->studentid) . And if the user click's no it should render the action that is supposes to render. Thank you in advance

Yes it is possible. You may use jQuery to accomplish it. You have to capture the "click" event over the button, and when it happens, you ask for the "confirm". If the user clicks "yes" in the confirmation dialogue, do a href to some controller/action, if the user clicks "no", do some other href to some other controller/action. In other words:

<script>
   $("#createstudent").click(function() {
     if(confirm('Do you want to register the student to a class?')){
       location.href = "BASEURL/controllerOne/actionOne";
     }
     else{
       location.href = "BASEURL/controllerTwo/actionTwo";  
     }
  });
</script>

Where BASEURL is the base url of your project in the web domain where you are developing it at (localhost if you are working on your localhost), controllerOne is the controller you request in case the user clicks "yes", and actionOne is the action of the controller "controllerOne" called when the user clicks "yes". Same for controllerTwo and actionTwo.

EDIT: You are using a submit button. It has its own href action (the action of the form). So, you may need to create your own button using a "a" link. For example:

<a id="createstudent" style="margin-left: 8px; margin-top:-10px; padding: 5px; background-color: #EA7A00; color: white;  width: 62px; height: 25px; border: 1px solid #4B1800;cursor: pointer;">Example</a>

Once you click on it, it href you to the correct action according to the option you choose (yes or no).

You will have to edit the CSS styles as your desire, of course.

Thanx for the answer Mundo. I think I have solved it by doing this:

<script>
$("#createstudent").click(function(e){
    e.preventDefault();

bootbox.confirm("<?php echo Yii::t('default', 'Do you want to register this student?');?>", function(result) {
  if(result){
       $('#student-form').submit();
      window.location = 'http://localhost/myproject/index.php/contract/studentcreate/'+'<?php echo $maxid ?>';

  } else {
     console.log("Confirmed: "+result);
      $('#student-form').submit();
  }

 }); 
});
</script>

I will try your solution too! Thanks again for the help!

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