Welcome Guest, Not a member yet? Register   Sign In
repopulate the form with selected value in dropdown list
#1

[eluser]Jeyakumar[/eluser]
Hi,

In my form I have few text fields and a country drop down list box. I validate all the controls by form_validation. While any error means, the control return to the form with the submitted values. But how i show the drop down box with the previous selected value?.
#2

[eluser]sore eyes[/eluser]
Hi, I'm not sure what you mean, but have you tried using the 'set_value', for example
Code:
<input type="text" name="yourname"  value="<?php echo set_value('yourvalue'); ?>
#3

[eluser]Jeyakumar[/eluser]
Thanks sore eyes.

Its for test box.

but, how do this for dropdown list box.
#4

[eluser]Pascal Kriete[/eluser]
Use set_select instead of set_value.
#5

[eluser]kgill[/eluser]
You use the selected property on the option so you end up with:

Code:
<select name="somename">
<option value="val1">display val1
<option value="val2" selected>display val2
<option value="val3">display val3
</select>

If you're using the form helper then you want the set_select() function, if you're building it yourself then as you build it just test if the value is equal to the submitted value if it is, add the selected property.
#6

[eluser]RS71[/eluser]
how do I use the form_dropdown with the set_select?
#7

[eluser]Pascal Kriete[/eluser]
form_dropdown accepts a value as it's third parameter, so In that case you're back to using set_value:

Code:
$month_names    = array('01' => lang('cal_january'), '02' => lang('cal_february'), /* ... */ );
echo form_dropdown('month', $month_names, set_value('month'), 'class="select_field_date"');
#8

[eluser]Future Webs[/eluser]
http://ellislab.com/forums/viewthread/97495/

that may help
#9

[eluser]RS71[/eluser]
I had previously tried that method and had no luck. I must be doing something wrong, could you help me out?

here's my code:

Code:
$data['sex_field'] = 'sex_field';
        $data['sex_options'] = array(
                                    ''  => 'Your Sex:',
                                    '1' => 'Female',
                                    '2' => 'Male'
                                );

Code:
&lt;? echo form_dropdown($sex_field, $sex_options, set_value($sex_field) ); ?&gt;
#10

[eluser]Future Webs[/eluser]
Code:
echo form_dropdown('example_dropmenu', $drop_menu_example_cats, set_value('example_dropmenu', (isset($rs_example->example_dropmenu)) ? $rs_example->example_dropmenu : ''))

that was my example code

$drop_menu_example_cats is an array passed to the view as part of the data array

I updated that post a while ago to fix a few problems and add a few new things i had sussed out




Theme © iAndrew 2016 - Forum software by © MyBB