Dynamically Loading Forms On A Page

I am trying to build a page where multiple forms need to be shown one after another (similar to linkedin boxes asking for recommendations about contacts). After submit button is pressed for one form, its place is taken by another form asking different details from the user.

I am pretty new to yii and don’t know how to proceed for this project. I don’t know how to swap one from after another. Could someone please help me?

Multiple ways to do this. A most used option is to use ajax request actions from your controller and use ‘renderAjax’ to render the relevant view(s) for the various forms.




use yii\helpers\Json;

public function processMultiForm($step = 1) {

   // Carry out your model settings and other validations

   $params = ['step' => $step, '<any_other_param>' => '<any_other_value>'];

   $output = $this->renderAjax('_form', $params);

   echo Json::encode($output);

}



The rest of the code must be in your view file (using client scripts) for each step taking the output from the ajax call and embedding in an element on the page.




// _form view

if ($step === 1) {   

   $form = ActiveForm::begin(['id' => 'form-1', 'other_options');

   // your form # 1 rendering fields

   // make an ajax call with step=1 on form submit action to 

   // replace content in an element on the same page and proceed 

   // to step 2

   ActiveForm::end();

}

elseif ($step === 2) {

   // your form # 2

}

elseif ($step === n) {

   // your form # n

}