In Yii2, From A Form, How Do You Pass Variables To Controller Actions?

I have a question. In YII2, from a form, how do you pass variables to controller actions? I’ve tried to do that with Html::submitButton link, but it doesn’t worked.

post your form and action code here

Ok, i’m trying to make a survey tool.

So, i have a Model of the survey which reference to N questions. This is part of this model:


<?php


namespace app\models;


use Yii;


    /**

     * @return \yii\db\ActiveQuery

     */

    public function getIdmateria0()

    {

        return $this->hasOne(Monmateria::className(), ['id' => 'idmateria']);

    }


    /**

     * @return \yii\db\ActiveQuery

     */

    public function getMonpreguntas()

    {

        return $this->hasMany(Monpregunta::className(), ['idencuesta' => 'id']);

    }

}

Of course, the questions’s model Monpregunta references the survey’s model. Then i have a 1 to N relations between the survey and his questions.

In my ActionView i have a survey with N questions on it. Then i call a view of survey, which have a Create Form View rendered on it, in order to create the N answers of the survey. So, once question is responded, I need to know which number of question was. I need to keep the track of which number of questions i am responding, but for it i need to send a parameter from the form to my controller. But i do not know how to do it.

The Controller (here i need to keep the track of which questions is responded)


  public function actionView($id)

    {

        //this is my id's survey       

        $model = $this->findModel($id);        

        //here i pre create my answer 

        $modelResultado = $this->crearResultado($id);        

        //here i call the view with a Create Form embedded. When the form action it's triggered  

        return $this->render('view', [

            'model' => $model,

            'modelResultado'=>$modelResultado,

            'orden'=>$model->orden,

        ]);

    }

//create the answer and setting the reference

 protected function crearResultado($id) {

        $modelResul = new \app\models\Monresultado;

        $modelResul->idmonresultadocab = $id;

         if ($modelResul->load(Yii::$app->request->post()) && $modelResul->save()) {

            //flash

        }

        return $modelResul;

    }



The view


<?php


use yii\helpers\Html;

use yii\widgets\DetailView;


/**

 * @var yii\web\View $this

 * @var app\models\Monresultadocab $model

 */




?>

<div class="monresultadocab-view">




    <?= DetailView::widget([

        'model' => $model,

        'attributes' => [

            'id',

            'idencuesta',

        ],

    ]) ?>

    <?php

    echo $this->render('_formResultado', [

        'model' => $modelResultado

      ]); ?>

</div>

This is the _formResultado view, and here i wish to tell to the action which id’s questions has been responded but i don’t know how to send the parameter to the action!!


<?php


use yii\helpers\Html;

use yii\widgets\ActiveForm;

use yii\helpers\ArrayHelper;


/**

 * @var yii\web\View $this

 * @var app\models\Monresultado $model

 * @var yii\widgets\ActiveForm $form

 */

?>


<div class="monresultado-form">


    <?php $form = ActiveForm::begin(); ?>


     

    <?= $form->field($model, 'idrespuesta')->dropDownList(ArrayHelper::map(\app\models\Monrespuesta::find()->all(),'id','nombre'))?>           

            

            

    

    <?= $form->field($model, 'libre')->textInput(['maxlength' => 2000]) ?>


    <div class="form-group">

        [b]<?= Html::submitButton($model->isNewRecord ? 'Siguiente' : 'Siguiente', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>[/b]

    </div>


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


</div>

If i send a parameter in the Html::submitButton tag, it doesn’t work.

Thanks

i thought you have to pass model id(while rendering corresponding view to know which id responded) into the action view of controller like:

         return &#036;this-&gt;render('view', [


        'model' =&gt; &#036;this-&gt;findModel(&#036;id),