Search code examples
phpcodeigniterselectrepopulation

Populating and repopulating select dropdown codeigniter


I would like to populate a select dropdown using codeigniter form_dropdown for that I have written below code in my view:

                 $experience = array(
                    "" => "- Min Exp -",
                    "0" => "Freshers & <1 Yr",
                    "1" => "1 Yr",
                    "2" => "2 Yrs",
                    "3" => "3 Yrs",
                    "4" => "4 Yrs",
                    "5" => "5 Yrs",
                    "6" => "6 Yrs"
                );

$js = 'id="minexp" style="width: 95%;padding-top: 7px" required';
echo form_dropdown('minexp', $experience, $this->input->post('minexp'), $js);

The repopulating is working perfect, but by default it is selecting the Freshers & <1 Yr and I would like to have - Min Exp - as selected when the user views the page first time.

Any help or suggestion would be a great help.. I googled and not found any solution..


Solution

  • Try checking if the post data is there first.

    echo form_dropdown('minexp', $experience, (($this->input->post('minexp')) ? $this->input->post('minexp') : ''), $js);