2
votes

I have a form in which there are two selectboxes, The second selectbox depends of the first one. All form data are saved in session because there is a back button in order user to be able to change things after firs submitting. So when user click's the back button I am able to assign the session value to the first dropdown but not to the second one which depends from the first one.

if(isset($session['form_1']['state_code']))

{ $state_code = $session['form_1']['form_1']; $this->registerJs('$("select#state_select").trigger("change");');} else { $state_code = " "; }

     echo $form->field($model, 'state_code')->dropDownList($states, 
      [ 'prompt' => ' Select state...', 
        'options' => [$state_code => ['Selected'=>'selected']],
        'onchange' => '
        $.get("'.Yii::$app->urlManager->createUrl('city?id=').
       '"+$(this).val(),function( data )  { 
          $("select#city").html( data ); 
        });'


      ]);

This code works for the first drop down

And the bellow code you can see the other drop down which does not work:

 if(isset($session['form_2']['city_select'])) 
        { $c_id = $session['form_2']['city_select']; } 
        else{ $c_id = ''; }
      echo $form->field($model, 'city_select')->dropDownList(['0' => 'Please select state..'], 
       [

          'options' => [$c_id => ['Selected'=>'selected']],

       ]);

Any Idea ?

1

1 Answers

0
votes

As an alternative you can use Kartik extension which solve your problem.