![]() |
Multiselect problem - 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: Multiselect problem (/showthread.php?tid=16805) |
Multiselect problem - El Forum - 03-17-2009 [eluser]Marcel Karras[/eluser] Hello, can someone assist me in using multiselects with CodeIgniter? I tried to implement it the way shown in the documentation but I only get the last selected element inside the $_POST request. My form_dropdown function: Code: echo form_dropdown ( 'installation', The $data['installation'] array element can be one or more elements so I added the multiple property in order to force the form_dropdown function to make multiple selections possible. Is there a more common way to enable the multiselect property without setting more than one default selection? When I var_dump my $_POST array variable I can see a 'installation' index but it is not an array but a string with the selection option value of the last selected item. Any idea how to solve this issue? Thanks in advance! Multiselect problem - El Forum - 03-17-2009 [eluser]xwero[/eluser] I guess the next line is causing your headache Code: $multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : ''; I would change Code: $multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : ''; Code: $form = '<select name="'.$name.'"'.$extra.">\n"; Multiselect problem - El Forum - 03-17-2009 [eluser]Marcel Karras[/eluser] This form_helper modification would not solve my problem as the $multiple variable will only be set to ' multiple="multiple"' if I don't set it manually using the $extra parameter. As I do set it manually there's no change to me. Multiselect problem - El Forum - 03-17-2009 [eluser]xwero[/eluser] My modification removes the $multiple line entirely. The only way to add the attribute is to put it in the extra parameter. IMO the count of the selected options makes no sense as the user is free to choose none or one option(s). It's not up to the html generating function to decide whether it is a single or a multiple dropdown. It would more sense if the multiple variable is selected as follows Code: $multiple = (strpos($extra, 'size') !== FALSE && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : ''; Multiselect problem - El Forum - 03-17-2009 [eluser]Marcel Karras[/eluser] I think the problem I have doesn't have something to do with the form_dropdown(...) function as the HTML form output is okay. Also the multiselect behavior works as expected but the POST request will only contain a string instead of an array containing all selected elements. If I change the name of the dropdown to "installation[]" I get a string with the value "Array" as POST result. So where does CodeIgniter add the selection result variable (should be an array but is a string) into the POST? Seems to me if something alters the $_POST before I get rid of it. Multiselect problem - El Forum - 03-17-2009 [eluser]xwero[/eluser] sorry i was looking at the wrong problem ![]() I guess you echoed the result, try print_r instead. The php parser treats everything as a string if you output it. false becomes 0, true becomes 1, array(1.2) becomes Array. Your solution, adding the square brackets, is correct. Multiselect problem - El Forum - 03-17-2009 [eluser]Marcel Karras[/eluser] I finally found the problem. ![]() After having compared various print_r($_POST) dumps I recognized that the form_validation->run() function alters my $_POST['installation'] element. So I checked my validation function and saw that there was a "trim" rule set that was a result of a dirty copy&paste;. ![]() Thank you for your assistance. Multiselect problem - El Forum - 04-28-2009 [eluser]markup2go[/eluser] Care to share your results? I've been having trouble use form_dropdown as a multiple select + validation + set_select(). Can anyone be so kind as to post some example usage for this? This should be covered in the user manual in my opinion. Ideally the select should take an array of key=>val pairs. It should be able to be validated. It should auto select options that were selected before a validation error. It should be able to set_select with an array from the database (for edit views). Thank you much! Multiselect problem - El Forum - 04-29-2009 [eluser]markup2go[/eluser] Well after some testing I finally got it down. In the view: (In this example $users contains array of id/usernames, $selected is $this->input->post('users')) Code: <?=form_dropdown('users[]', $users, $selected, 'multiple="multiple"')?> In the controller: Code: $rules = array('users' => 'required'); You will probably want to check for $_POST before you assign $selected. Multiselect problem - El Forum - 06-15-2009 [eluser]bob.rosset[/eluser] So you just added a pair of [] at the end of dropdown name! Thanks for the solution, i've been working on that a lot today! |