Welcome Guest, Not a member yet? Register   Sign In
how to use set_select() with form validation when form_dropdown() is populated by an array
#1

[eluser]Flying Fish[/eluser]
From what I understand from the user guide, to auto select a dropdown after failed form validation you need to do something like this

Code:
<option value="one" &lt;?php echo set_select('myselect', 'one', TRUE); ?&gt; >One</option>


But I'm using a for loop to automatically generate the quantity for my dropdown

then I'm passing the array to the form_dropdown() function to dynamically generate my option tags

Code:
$order_quantity_inc250 = array(250 => 250);
        for ($i=250; $i<=15000; $i+=250)
        {
            $order_quantity_inc250[$i] = $i;
        }


<div>
&lt;?
echo $order_quantity_label;
echo form_dropdown("order_quantity", $order_quantity_inc250, '', 'id="order_quantity"');
echo form_error('order_quantity');
?&gt;
</div>

Does anyone know how I can still auto select and create the dropdown with the CI form helper?

Thanks!
#2

[eluser]jedd[/eluser]
Code:
echo form_dropdown("order_quantity", $order_quantity_inc250, '', 'id="order_quantity"');

The empty string should be your selected item - what's the variable name you've got that set to? - and it shouldn't be a problem given the number gets generated again dynamically.
#3

[eluser]Flying Fish[/eluser]
Thanks for the reply jedd. Not sure what 'variable name' you were referring to above, could you clarify for me?

the html generated by the form dropdown looks something like this...
[code]
<select id="order_quantity">
<option value="250">250</option>
<option value="500">500</option>
<option value="750">750</option>
&lt;!-- continues to increment by 250's --&gt;
</select>
[code]

I know if I put a value in the 3rd parameter, that it will select an option by default when the form is loaded

example: the code below would select the <option value="250">250</option>
[code]
echo form_dropdown("order_quantity", $order_quantity_inc250, '250', 'id="order_quantity"');
[code]

but let's say a user submits a form and it doesn't pass validation, I want to make sure the option they selected stays selected when the page refreshes. I don't want it to revert to the default value

for example: they select a quantity of 1000 and then fail validation, I don't want the dropdown to then select 250

from what I understand though, to accomplish that I need to insert this code into each <option> tag
[code]
&lt;?php echo set_select('order_quantity', '250')?&gt;
[code]

but I'm not sure how to do it...does that make sense?
#4

[eluser]TheFuzzy0ne[/eluser]
Code:
echo form_dropdown("order_quantity", $order_quantity_inc, $this->input->post('order_quantity'));
#5

[eluser]Flying Fish[/eluser]
Perfect! Thanks much!
#6

[eluser]InvalidCharacter[/eluser]
What can you do when you need a default value on the first page load, but need to re-populate with the user selection if they do not pass validation?

Code:
$states = array(
                'ACT' => 'ACT',
                'NSW' => 'NSW',
                'NT'  => 'NT',
                'QLD' => 'QLD',
                'SA'  => 'SA',
                'TAS' => 'TAS',
                'VIC' => 'VIC',
                'WA'  => 'WA'
                );
echo form_dropdown('state[]', $states, 'QLD');

Do I need to write out HTML <select> myself?

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>
#7

[eluser]InvalidCharacter[/eluser]
I'm also having much the same problem with checkboxes set to be checked by default. How do I get them to uncheck if the user unchecks and does not pass validation. I know I can write my own routine, but is there a way to do it with the form class or helper?
#8

[eluser]TheFuzzy0ne[/eluser]
For your select box, you might want to try this:

Code:
echo form_dropdown('state[]', $states, set_value('state[], 'QLD')); # Defaults, to QLD if the form has not been submitted.

But I don't think this will work as you appear to want to allow a multi-select? If that's not the case, you don't need the square brackets after state[].
#9

[eluser]Evil Wizard[/eluser]
Code:
&lt;?=form_dropdown(array('name'=>"static[{$assoc_array['loop_id']}][key]", 'id'=>"type_{$assoc_array['loop_id']}",'class'=>'text', 'style'=>'width:150px;'), $states, set_select(array('static',$assoc_array['loop_id'],'key'), $states['QLD']));?&gt;
That's how to access multi dimensional arrays with the set_select, it does work, I'm using a similar line on some of my pages. Tongue
#10

[eluser]InvalidCharacter[/eluser]
Thanks guys. It makes sense now that I see it. Wink




Theme © iAndrew 2016 - Forum software by © MyBB