Reputation: 249
Hey everyone I am trying to set selected="selected" for drop down list but cant seem to get to solution.
E.g.. A city is entered in user's table when user chooses to edit his profile the rest of the details comes in text boxes but the city should be displayed in drop down list and the default selected value should match with the city of that user.
Note :- The citites are entered directly in like this
echo $this->Form->input("city",array("type"=>"select","empty"=>"City","options"=>array("city1"=>"city1","city2"=>"city2","city3"=>"city3","city4"=>"city4")));
Upvotes: 0
Views: 356
Reputation: 2588
You can use default
to make any option as selected in cakephp
Try This:
echo $this->Form->input('city', array('type' => 'select', 'options' => array("city1"=>"city1","city2"=>"city2","city3"=>"city3","city4"=>"city4"), "default" => "city1"));
Upvotes: 1
Reputation: 566
You don't need to do anything for selected value. Cakephp render automatically fields name. so if in the user user table city field name is city_id
than you need to write below statement:
<?php echo $this->Form->input('city_id', array("label" => false, 'type' => 'select', 'options' => $cities, "empty"=>"Select City", "div" => false )); ?>
But if your data is not rendered from cities table, then you need to write the same column name of the select box:
<select name="city">
<?php foreach ($city_array as $key => $value) { ?>
<option <?php echo ($value == $default) ? 'selected' : '' ?> value="<?php $value ?>"><?php echo $value</option>
<?php } ?>
</select>
The third option to get select value like below:
<?php echo $this->Form->input('city', array(
'type' => 'select',
'options' => array('city1' => 'city1', 'city2' => 'city2', 'city3' => 'city3'),
'selected' => 'city1'
));
?>
Upvotes: 0