Welcome Guest, Not a member yet? Register   Sign In
form_dropdown repopulate
#1

[eluser]JanDoToDo[/eluser]
Hey guys,

Im wondering how to reselect a submitted dropdown item if the form validation fails using the form helper.

Obviously it can't be done the standard way with set value as the html isnt written - its done using the form helper?
#2

[eluser]must[/eluser]
there is set_select() available in the form helper check it out in the user guide
Example:
Code:
<select name="myselect">
    <option value="one" &lt;?php echo set_select('myselect', 'one', TRUE); ?&gt; >One</option>
    <option value="two" &lt;?php echo set_select('myselect', 'two'); ?&gt; >Two</option>
    <option value="three" &lt;?php echo set_select('myselect', 'three'); ?&gt; >Three</option>
</select>

check http://ellislab.com/codeigniter/user-gui...elper.html

it can also be done by set_value() but it's pretty much the same principal
Code:
<select name="some_select">
&lt;?php
     $some_array = array('value1', 'value2', 'value3');
     $chosen_value = set_value('some_select');

     foreach($some_array as $value):
        if ($chosen_value == $value):
            $checked = "selected=\"selected\"";
        else:
            $checked = "";
        endif;
?&gt;
     <option value="&lt;?php echo $value?&gt;" &lt;?php echo $checked?&gt;>&lt;?php echo $value?&gt;</option>
&lt;?php endforeach;?&gt;
</select>

And there are a lot of other ways to exploit this
#3

[eluser]JanDoToDo[/eluser]
But I can't do that If i make a dropdown with the form helper, i.e

form_dropdown('select_options', $options, $selected);
#4

[eluser]must[/eluser]
i never use form helper to help me generate html input ... but looking into the documentation i see that you can set a value to be selected in the third parameter. it can contain the value that you want to be selected by doing something like :
Code:
$values = array(....);
form_dropdown('select_name', $values, set_value('select_name'));

and if you look to the form_helper.php you can notice that this would be done automatically if you do not set a select value (the third parameter)




Theme © iAndrew 2016 - Forum software by © MyBB