Get selected value in dropdownlist and use it for checkboxlist

Hi. I have these models:

  1. Section (this holds the information of every section)

  2. SubjSchedule (this holds all the schedules of every subject)

  3. SubjSection (which holds all the subject schedule in each section)

I want to add a subject schedule in a certain section, so I have a dropdownlist that generates all sections available. Then from that, I will generate a checkboxlist that will show all schedules of every subject.

I created this in my subj-section/_form.php




<div class="subj-section-form">

    

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

    

    //generates all sections in a dropdownlist

    <?= $form->field($model, 'section_id')->dropdownList(

           ArrayHelper::map(Section::find()->all(), 'section_id', 'section_code')

    ) ?>


    

    <?php $data = ArrayHelper::map($data, 'subjsched_id', 'subjsched_summary'); ?>

    

    //this the checkboxlist

    <?= $form->field($model, 'subjsection')->checkboxList($data, [

        'class' => "btn-checkbox options",

        'data-toggle' => "button",

        'item' => function($index, $label, $name, $checked, $value) {

            $checked = $checked ? 'checked' : '';

            return "<p><input type='checkbox' {$checked} name='{$name}' value='{$value}'>{$label}</p>";

        }])->label('Choose Subjects');?>



This is my subj-section/create.php




    public function actionCreate()

    {

        $model = new SubjSection();


        $data = SubjSchedule::find()->all();

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

            $subjectsectionList = $_POST['SubjSection']['subjsection'];

            

            foreach($subjectsectionList as $value){

                $newmodel = new SubjSection();

                $newmodel->section_id = $model->section_id;

                $newmodel->subjsched_id = $value;

                $newmodel->save();

            }


            $searchModel = new SubjSectionSearch();

            $dataProvider = $searchModel->search(Yii::$app->request->queryParams);


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

                'searchModel' => $searchModel,

                'dataProvider' => $dataProvider,

            ]);


        }


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

            'model' => $model,

            'data' => $data,

        ]);

        

    }



[b]My problem is when I want to update the subject schedules under a section, the checkboxes are not selected. I found out from another thread that I just have to put this:


$model->subjsection = ArrayHelper::map(SubjSection::find()->where(['section_id' => $id])->all(), 'subjsect_id', 'subjsched_id');

.

However, I need to check which section was chosen from the dropdownlist but I can’t get the value. How do I get the selected value from the dropdownlist so I could use it as a filter on my checkboxlist?

thank you so much in advance![/b]

Try this:




$list = [1=>'Opt1','2'=>'Opt2','3'=>'Opt3'];

$checkeded = [1, 2]; //selected values

$model->subjsection = $checkedList;


$form->field($model,'subjsection')->checkboxList($list);



Hi! I’ve already done this in my code as I’ve described above (just using my $model instead). What I wanted to know is how to get the value of the dropdownlist because the checked values in my checkboxlist is dependent on the value chosen in the dropdownlist. I just don’t know how to get the value chosen in the dropdownlist so I could use the value. Is there anyway? Thank you btw. :)