I'm trying to build a form with two dropdown lists. Both lists have the same source (populated by a database query). The user must select two different values. If they are the same, form validation must display an error message.
I load the form helper and the form_validation library.
On open, the form must display a default value in both dropdown lists. If the user makes the wrong selection, the form must be shown again, with the error message, and the faulty selection in the dropdowns.
In my controller I have this code:
PHP Code:
$cities = array(
0 => '-- select a city --',
1 => 'New York',
2 => 'Dallas',
3 => 'Washington DC'
);
$mycity = 2;
$data['fields'] = array(
'city' => array(
'name' => 'city',
'options' => $cities,
'selected' => set_select('cities',$mycity,0)
)
);
$this->load->view('select_city',$data);
In my view:
PHP Code:
echo form_dropdown($fields['city']);
Problem: the default choice ('Dallas') does not appear at all. The dropdown itself does contain the list of cities, but none is selected.
How can I do this the right way?