pepe
pepe

Reputation: 9909

PHP CodeIgniter - Dropdown using foreach does not show "selected" value

I am generating options for my dropdown using

// view.php
foreach ($plants as $row):
    $options[$row->plant_id] = $row->plant_name;
endforeach;

and then lower in the HTML part of view.php

//view.php
$js = 'onChange = "plantDateDelete(\'/size/get_dates_for_plant/\'+this.value);"';
echo form_dropdown('plant_id', $options, 'Select', $js);

The dropdown show options OK, but it does NOT show 'Select' as the "selected"/default value. It shows up with the first option of the array instead.

The HTML source also shows 'Select' in form_dropdown was ignored.

I really need this dropdown to show up with 'Select' as default so as to force the user to activate the onChange function.

Any idea what is going on here or how to solve this issue?

Upvotes: 2

Views: 2471

Answers (1)

Jacob
Jacob

Reputation: 8334

You need to have the default option in your $options array...

So before your foreach, do:

$options = array('Select');

I should add, this will have the value of 0 in the dropdown, but as its the first element in the array it will be selected by default, unless another option is passed as the default value.

If you wanted to explicitly set this value as the default you would pass 0 as the default argument.

Upvotes: 7

Related Questions