doqrjrc95405 2013-11-24 10:49
浏览 53

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

  • 写回答

1条回答 默认 最新

  • douzhicai7148 2013-11-24 12:34
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大