如何在POST表单中放入ID(YII2)

时间:2018-01-09 12:20:09

标签: php yii2

我是Yii2的新手,现在我尝试建立租车系统,尤其是练习。现在我尝试下订单。我怎么能把汽车的ID放在POST表格中?我的代码有表格和Car的描述如下。 enter image description here

<div class="container">
<div class="row">
    <div class="col-lg-3">
        <?php
        if(Yii::$app->session->hasFlash('success')){
            echo Yii::$app->session->getFlash('success');
        }
        if(Yii::$app->session->hasFlash('error')){
            echo Yii::$app->session->getFlash('error');
        }
        $form = ActiveForm::begin([
            'id' =>'order-form',
            'action' => ['site/contract'],
            'options' => ['class' => 'form-horizontal']
        ]) ?>
        <?= $form->field($model, 'first_date') ?>
        <?= $form->field($model, 'second_date') ?>

        <div class="form-group">
            <div class="col-lg-offset-1 col-lg-11">
                <?= Html::submitButton('Order', ['class' => 'btn btn-primary']) ?>
            </div>
        </div>
        <?php ActiveForm::end() ?>

    </div>
    <!-- /.col-lg-3 -->
    <div class="col-lg-9">
        <div class="card mt-4">
            <img class="card-img-top img-fluid" src="<? echo $car->getImage()?>" alt="">
            <div class="card-body">
                <h3 class="card-title"><? echo $car->mark?> <? echo $car->model?></h3>
                <h4 style="color: red">$<? echo $car->price?> per day</h4>
                <p class="card-text"><? echo $car->description?></p>
                <span class="text-warning">&#9733; &#9733; &#9733; &#9733; &#9734;</span>
                4.0 stars
            </div>
        </div>
        <!-- /.card -->

    </div>
</div>

这是我的控制器,但现在它是纯粹的:

public function actionContract() {
    $model = new OrderForm();

    if($model->load(Yii::$app->request->post()) && $model->validate()){
        Yii::$app->session->setFlash('success','Заявка отправлена');
    }
    else {
        Yii::$app->session->setFlash('error','Проверьте данные');
    }
    return $this->redirect(['site/view']);
}

1 个答案:

答案 0 :(得分:1)

您可以将以下隐藏的输入字段添加到表单中:

<?= $form->field($model, 'id')->hiddenInput(['value'=> $value])->label(false); ?>

or

<?= Html::hiddenInput('id', $value); ?>

当您处理表单时,只需像普通字段一样检索ID,此处$value是您要传递的ID