简体   繁体   中英

Passing data from view to controller in yii

I have a text field in my view and I want to pass the value of textbox to the controller, but I don't know how to do it.

I have tried googling it but it only gives ides about passing data from conntroller to view in yii, so please give an example of doing this with ajax.

follow the steps:

in form:

<div class="form-group">
<?php echo $form->labelEx($model,'order_id', array('class' => 'control-label col-lg-4')); ?>
<div class="col-lg-8">
    <?php echo $form->textField($model,'order_id',array('class' => 'form-control',
    'ajax' =>
        array('type'=>'POST',
            'url'=>$this->createUrl('recieveValue'), // write in controller this action
            'update'=>'#price',
            'data'=>array('order_id'=>'js:this.value'),
        )

    )); ?>
</div>
<?php echo $form->error($model,'order_id'); ?>

In controller:

public function actionRecieveValue(){
echo $_POST['order_id'];
}

At the top of same controller:

array('allow', // allow authenticated user to perform 'create' and 'update' actions
            'actions'=>array('create','update','recieveValue'),
            'users'=>array('@'),
),

Explanation:

Here text field id is order_id , controller action is recieveValue what I wrote in the ajax URL as 'url'=>$this->createUrl('recieveValue'), . Go to the controller and write action name as actionRecieveValue just add action before recieveValue . Now go to the top of the controller in the method accessRules and allow it recieveValue into array. Now check through firebug console. Type something into the text box and move the mouse from the textbox. You will find that your textbox value will be recieved into the controller.

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