简体   繁体   中英

Yii2: How to send data from one page to another?

On roompage.php page I have data like :

<?php $form = ActiveForm::begin(['action' =>['site/checkout'],'method' => 'POST']); ?>
<?= $form->field($model, 'room_type')->hiddenInput(['value'=>$data->room_type])->label(false); ?>
<?= $form->field($model, 'rate')->hiddenInput(['value'=>$data->rate])->label(false); ?>
<?= Html::submitButton('CHECKOUT', ['class' => 'button btn btn-default']) ?>
<?php ActiveForm::end(); ?>

I need to pass this data to checkout.php page How to do this? Can I send data through URL ? If yes then how to do this or any other way to do the same?

public function actionCheckout()
    {      
        $model = new CreateBookings();
        $data = Yii::$app->request->post();
    if(isset($data['DynamicModel'])) 
    {
    $room_type = $data['DynamicModel']['room_type']; 
    }
        if ($model->load(Yii::$app->request->post()))
        {
            $model->save();
            return $this->redirect('confirmation');
        } 
        else 
        {
            return $this->render('checkout', [
            'model' => $model,'room_type' => $room_type]);
        }
    }

public function actionRoompage($room_id)
    {   $model = new \yii\base\DynamicModel(['room_type','rate']);
        $searchModel = new RoomTypesSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        $data = RoomTypes::find()->where(['room_id' => $room_id])->one();
        return $this->render('roompage', [
            'dataProvider' => $dataProvider,
            'searchModel' => $searchModel,
            'data' => $data,
        ]);
    }

Need to pass data from actionRoompage() to actionCheckout()

You should use POST request to send some data to another page in this case. In actionCheckout you are already ready to get POST data and load it ot the model $model->load(Yii::$app->request->post())

You also need to create form in your view roompage.php For example, in your actionRoompage also create $model = new CreateBookings();

And than in view display inputs(you can make them hidden):

<?php $form = ActiveForm::begin(['id' => 'checkout-form']); ?>
<?php echo $form->field($model, 'quantity') ?>
<?php echo $form->field($model, 'email') ?>
<?php echo Html::submitButton(Yii::t('frontend', 'Login'), ['class' => 'btn btn-primary', 'name' => 'login-button']) ?>
<?php ActiveForm::end(); ?>

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