![]() |
how to use set_select() with form validation when form_dropdown() is populated by an array - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: how to use set_select() with form validation when form_dropdown() is populated by an array (/showthread.php?tid=16906) |
how to use set_select() with form validation when form_dropdown() is populated by an array - El Forum - 03-19-2009 [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" <?php echo set_select('myselect', 'one', TRUE); ?> >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); Does anyone know how I can still auto select and create the dropdown with the CI form helper? Thanks! how to use set_select() with form validation when form_dropdown() is populated by an array - El Forum - 03-19-2009 [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. how to use set_select() with form validation when form_dropdown() is populated by an array - El Forum - 03-19-2009 [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> <!-- continues to increment by 250's --> </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] <?php echo set_select('order_quantity', '250')?> [code] but I'm not sure how to do it...does that make sense? how to use set_select() with form validation when form_dropdown() is populated by an array - El Forum - 03-20-2009 [eluser]TheFuzzy0ne[/eluser] Code: echo form_dropdown("order_quantity", $order_quantity_inc, $this->input->post('order_quantity')); how to use set_select() with form validation when form_dropdown() is populated by an array - El Forum - 03-20-2009 [eluser]Flying Fish[/eluser] Perfect! Thanks much! how to use set_select() with form validation when form_dropdown() is populated by an array - El Forum - 06-02-2009 [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( Do I need to write out HTML <select> myself? Code: <select name="myselect"> how to use set_select() with form validation when form_dropdown() is populated by an array - El Forum - 06-02-2009 [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? how to use set_select() with form validation when form_dropdown() is populated by an array - El Forum - 06-03-2009 [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[]. how to use set_select() with form validation when form_dropdown() is populated by an array - El Forum - 06-03-2009 [eluser]Evil Wizard[/eluser] Code: <?=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']));?> ![]() how to use set_select() with form validation when form_dropdown() is populated by an array - El Forum - 06-03-2009 [eluser]InvalidCharacter[/eluser] Thanks guys. It makes sense now that I see it. ![]() |