0

I'm new to Yii framework.I'm using the form.php to update the fields of the table. So now I use this form with three submit buttons - [Save, Accept, Reject]. The form now has the following fields.

<div class="row">
    <?php //$model->ReviewedDate=date('Y-m-d H:i:s');?>
    <?php echo $form->labelEx($model,'ReviewedDate'); ?>
    <?php echo $form->textField($model,'ReviewedDate',array('value'=>'0000-00-00 00:00:00','readonly' => true));te  ?>
    <?php echo $form->error($model,'ReviewedDate'); ?>
            </div>
<div class="row">
    <?php echo $form->labelEx($model,'Approved'); ?>
    <?php echo $form->textField($model,'Approved'); ?>
    <?php echo $form->error($model,'Approved'); ?>
</div>    
<div class="row buttons">
        <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save',array('confirm' => 'Are you sure to save')); ?></div>

Above there is Approved field.Now, when I click on save all the other fields has to be updated except for approved. (Approved is 0 by default). So when I click on Approve button it should update Approved as well as other fields. When I click on Reject, it should update the approved field to 0. How can I do this.

3 Answers 3

3

You can use three submit button and can manage functionality as per that. In the form create 3 buttons as per below :

<?php echo CHtml::submitButton('Save', array('name' => 'save')); ?>
<?php echo CHtml::submitButton('Accept', array('name' => 'accept')); ?>
<?php echo CHtml::submitButton('Reject', array('name' => 'reject')); ?>

In the controller check which button is clicked as per below :

<?php 
if(isset($_POST['save'])){
    //save submit button is click and code for save button will be here    
}
if(isset($_POST['accept'])){
    //accept submit button is click and code for accept button will be here    
}
if(isset($_POST['reject'])){
    //reject submit button is click and code for reject button will be here    
} ?>

All the best :)

Sign up to request clarification or add additional context in comments.

1 Comment

<?=Html::submitButton('Disconnect', ['id'=>'d','name'=>'dco','class' => 'btn btn-primary']);?> and in contoller var_dump(isset($_POST['dco'])); die(); gives me bool(false)
0

Instead of having 3 submit buttons, I'd suggest you use a dropdown list so your users can pick the desired action. Then you check for the value of the dropdown control in order to either "Save", "Accept" or "Reject".

echo CHtml::dropDownList('action', '', array('Accept', 'Reject'));

And in your controller:

if (isset($_POST['ModelName'])) {
    switch ($_POST['action']) {
        case 'Accept':
            # code for Acceptance
            break;

        case 'Reject':
            # code for Rejection
            break;
    }
    //Continue with Saving the Model data here
}

1 Comment

<?=Html::submitButton('Disconnect', ['id'=>'d','name'=>'dco','class' => 'btn btn-primary']);?> and in contoller var_dump(isset($_POST['dco'])); die(); gives me bool(false)
0

You can add a hiddenField with the action:

<?php echo $form->hiddenField($model, 'typeSubmit'); ?> // Add 'typeSubmit' attribute on the model

And 3 submit buttons. Each button puts on the hidden field the type of Submit.

<?php echo CHtml::submitButton('Save', array('class'=>'btn','onclick'=>'$("#ModelName_typeSubmit").val("save");')); ?> // #ModelName = $model name class.
<?php echo CHtml::submitButton('Accept', array('class'=>'btn','onclick'=>'$("#ModelName_typeSubmit").val("accept");')); ?>
<?php echo CHtml::submitButton('Reject', array('class'=>'btn','onclick'=>'$("#ModelName_typeSubmit").val("reject");')); ?>

1 Comment

I tried this but does not work - <?php echo $form->hiddenField($model, 'Approved'); ?> <?php echo CHtml::submitButton('Save', array('class'=>'btn','onclick'=>'$("MessageTemplate_Approved").val("save");')); ?> <?php echo CHtml::submitButton('Accept', array('class'=>'btn','onclick'=>'$("MessageTemplate_Approved").val("accept");')); ?> <?php echo CHtml::submitButton('Reject', array('class'=>'btn','onclick'=>'$("MessageTemplate_Approved").val("reject");')); ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.