如何从窗体获取输入值到控制器并将其存储在变量中?

时间:2019-06-11 12:41:04

标签: yii2

我有一个表单页面,其中有3个输入字段连接到数据库,现在我需要添加一个新的输入字段并在控制器页面中获取变量中的值。

我尝试使用常量来使函数正常工作,但是我需要在函数中使用变量

我的表单页面

<?= $form->field($model, 'idnew_table')->textInput(['maxlength' => 5,'style'=>'width:100px']) ?>

<?= $form->field($model, 'adcaddress1')->textInput(['maxlength' => 5,'style'=>'width:200px']) ?>

<?= $form->field($model, 'adcaddress2')->textInput(['maxlength' => 5,'style'=>'width:200px']) ?>
<label>Value:</label><br>
   <input type="text" name="submitvalue" value=""><br><br>
<div class="form-group">
    <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>

<?php ActiveForm::end(); ?>

我的控制器功能是

public function actionCreate($data=null,$data1=null,$data2=null)
{
    $model = new Adcaddress();
    $this->layout='admin';
    if ($model->load(Yii::$app->request->post()) && $model->save()) {
        return $this->redirect(['view', 'id' => $model->idnew_table]);

    } else {

            $data2 = "variable";
            $my_file1 = 'test.txt';
            $handle1 = fopen($my_file1, 'r');
            $data  = $handle1;
            $data = fread($handle1,filesize($my_file1));           
            $my_file = 'file.txt';
            $handle = fopen($my_file, 'w') or die('Cannot open file:  '.$my_file);
            //$data = $handle;
            fwrite($handle, "$data2"); 
       // return $this->render('create', [                'model' => $model, ]);
             return $this->render('create', array('data' => $data,'data1' => $data1,'data2'=>$data2,
                             'model' => $model));
    }

在控制器页面的变量$ data2中,我需要name = submitvalue的值。

1 个答案:

答案 0 :(得分:0)

$model->load(Yii::$app->request->post())

仅加载与$ _POST中包含的模型相关的属性

您添加的名为“ submitvalue”的输入未声明为模型的一部分 (属性模型的名称默认为ModelName [attribute])

,然后不会自动加载到$ model

如果您需要$ _POST的内容并进行检查,可以使用

  $post  = Yii::$app->request->post(); 
  var_dump($post);
相关问题