dongying6179 2015-10-28 07:36
浏览 83
已采纳

Yii 2:使用post将变量传递给另一个视图的新表单

with yii 2 i want to pass 3 variables from a view to another view. In my controller i've created an action that get this 3 variables and then open a view, but in the "start" view i can't write a working form :/

This is my starting view:

<?= Html::beginForm(['preview', 'type'=>0, 'start'=>'start', 'end'=>'end'], 'post', ['enctype' => 'multipart/form-data']) ?>

                <?=
                    \yii\widgets\MaskedInput::widget([
                        'name' => 'start',
                        'clientOptions' => ['placeholder' =>  'GG-MM-AAAA'],
                        'mask' => '99-99-9999',
                    ])
                ?>

                <?=
                \yii\widgets\MaskedInput::widget([
                    'name' => 'end',
                    'clientOptions' => ['placeholder' =>  'GG-MM-AAAA'],
                    'mask' => '99-99-9999',
                ])
                ?>

                <?= Html::submitButton('Go', ['class' => 'submit']) ?>

<?= Html::endForm() ?>

and this is my action:

public function actionPreview($type, $start, $end)
{
    return $this->redirect(['preview', 'type' => $type, 'start'=>$start, 'end'=>$end]);
}

But, when i push the button "Go", Yii 2 returns

Bad Request (#400) - Missing required parameters: start,end

I've tried also this action:

public function actionPreview() //$type, $start, $end
{
    $post = Yii::$app->request->post();
    $type = $post['type'];
    $start = $post['start'];
    $end = $post['end'];
    return $this->redirect(['preview', 'type' => $type, 'start'=>$start, 'end'=>$end]);
}

But it returns this error:

Undefined index: type /start / end

Any ideas?

-------UPDATE--------

Now rendering works, but i can't read the params from the starting view.

MyController:

public function actionPreview() //$type, $start, $end
{
    $type = Yii::$app->request->get('type');
    $start = Yii::$app->request->get('start');
    $end = Yii::$app->request->get('end');
    return $this->render('preview', [
        'type' => $type, 'start'=>$start, 'end'=>$end
    ]);
}

The Form in my View is the same that you can see above, but it sends generic params to next page (in short, it doesn't read the input field):

start=start

end=end

  • 写回答

1条回答 默认 最新

  • dongliu1883 2015-10-28 07:50
    关注

    I have tried in this wy

        <?php 
    
        $start = '1234560123';
    
        ?>
        <?=   Html::beginForm(['preview', ], 'post', ['enctype' => 'multipart/form-data']) ?>
    
    
    
                         <?=   \yii\widgets\MaskedInput::widget([
                                'name' => 'start',
                                'value' => $start,
                                'clientOptions' => ['placeholder' =>  'GG-MM-AAAA'],
                                'mask' => '99-99-9999',
                            ])
                        ?>
                        <?= Html::submitButton('Go', ['class' => 'submit']) ?>
    
        <?= Html::endForm() ?>
    

    and work

    If you have the error undefined variable then there are problem in controller for passing the vars.

    For a correct view remeber the data in vars must match the mask...

    And for the preview you should use post and not get

    public function actionPreview() //$type, $start, $end
    {
        $type = Yii::$app->request->post('type');
        $start = Yii::$app->request->post('start');
        $end = Yii::$app->request->post('end');
        return $this->render('preview', [
           'type' => $type, 'start'=>$start, 'end'=>$end
       ]);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料