Master Yoda
Master Yoda

Reputation: 531

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.

Upvotes: 1

Views: 6581

Answers (1)

StreetCoder
StreetCoder

Reputation: 10061

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 recieveValuewhat 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.

Upvotes: 1

Related Questions