2

i have a form that uses a drop down and im using codeigniter , form helper and form validation so when i get a validation error in my form , all the correctly entered fields are populated using set_value of codeigniter , however this doesnt work for dropdown

im doing :

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

when get error in form , always the first value of dropdown is selected and not previously set

Any ideas , what am i doing wrong?

5 Answers 5

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

@AndrásRátz your method "echo form_dropdown('mid', $id_map, set_select('mid'), 'id="mid"');" is wrong mate - see my answer below
0
  <?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"');
                    ?>

Comments

0

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>";
                      }
      ?>

Comments

-2

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

echo form_dropdown('mid',$id_map);

is sufficient.

1 Comment

I don't know what the down vote is for. This code worked for me yesterday in making a form field "sticky" after the other methods failed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.