populate dropdown in codeigniter form using set_value

。_饼干妹妹 提交于 2019-11-30 16:36:15

The correct method to use set_select() is

<select name="myselect">
<option value="one" <?php echo set_select('myselect', 'one', TRUE); ?> >One</option>
<option value="two" <?php echo set_select('myselect', 'two'); ?> >Two</option>
<option value="three" <?php echo set_select('myselect', 'three'); ?> >Three</option>
</select>

So Andras Ratz method/answer won't work.

Your method is correct.

<?php echo form_dropdown('mid', $id_map, set_value('mid'), 'id="mid"') ?>

However remember that set_value() is only going to return the value if you have setup a validation rule for it, in your case for mid. You could try as below -

<?php echo form_dropdown('mid', $id_map, (isset($_POST['mid']) ? $_POST['mid'] : ''), 'id="mid"') ?>

or just set a validation rule for mid too

It's because you have to use the set_select() function , not the set_value();

You can check that here: http://codeigniter.com/user_guide/helpers/form_helper.html

And the right syntax:

EDIT :

<?php echo form_dropdown('mid', $id_map,  set_select('mid'), 'id="mid"'); ?>

If you have a default value, that should go in the 2. parameter for set_select.

I hope this will work.

  <?php // Change the values in this array to populate your dropdown as required
                    $options = array(
                    'active' => 'Active',
                    'inactive' => 'Inactive',
                    );
                    echo form_dropdown('mid', $options,$id_map, ' id="mid"');
                    ?>

What about if the the form select content is selected from a foreach loop like this

<?php foreach ($roles as $role) {
        echo "<option value=" . $role->id . ">" . $role->role_name . "</option>";
                      }
      ?>

If you do not have a validation rule on the actual dropdown field, then

echo form_dropdown('mid',$id_map);

is sufficient.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!