CodeIgniter Forums
dropdown with multiple - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: dropdown with multiple (/showthread.php?tid=71233)



dropdown with multiple - Nome - 07-22-2018

Hello, I wanted to use dropdown with multiple attribute using helper forms:
PHP Code:
<?= form_dropdown('dpd', array(
 
       '1' => 'First',
 
       '2' => 'Second',
 
       '3' => 'third',
 
       '4' => 'fourth',
), 
'2''class = "form-control" multiple');?>
PHP Code:
$drpdown $this->request->getPost('dpd'); 

I choose several values, expect to get something like an array...

but instead I get:
Code:
string '3' (length=1)

Yes, this value also participates in the sample, but it is chosen last. There should be 2 more.

What have I done wrong? Thanks!


RE: dropdown with multiple - Pertti - 07-22-2018

The name of the dropdown has to be array as well:
PHP Code:
form_dropdown('dpd[]', array(
// ... 

This only applies to HTML element, and when accessing POST variables, you only reference dpd, not dpd[].


RE: dropdown with multiple - Nome - 07-26-2018

(07-22-2018, 01:46 AM)Pertti Wrote: The name of the dropdown has to be array as well:
PHP Code:
form_dropdown('dpd[]', array(
// ... 

This only applies to HTML element, and when accessing POST variables, you only reference dpd, not dpd[].

Forgive me, I forgot about this, similar situation was with multi-uploading files.

Thank you!


RE: dropdown with multiple - dave friend - 07-26-2018

You could also use this.

PHP Code:
<?= form_multiselect('dpd[]', ['1' => 'First''2' => 'Second''3' => 'third''4' => 'fourth',], '2'"class = 'form-control'"); ?>



RE: dropdown with multiple - Nome - 07-27-2018

(07-26-2018, 03:09 PM)Друг друга. Wrote: You could also use this.

PHP Code:
<?= form_multiselect('dpd[]', ['1' => 'First''2' => 'Second''3' => 'third''4' => 'fourth',], '2'"class = 'form-control'"); ?>

Thank you, but what is the difference between an example taken from the documentation where the classical array record is used and the reduced record of the array from yours?


RE: dropdown with multiple - dave friend - 07-27-2018

(07-27-2018, 04:05 AM)Nome Wrote: Thank you, but what is the difference between an example taken from the documentation where the classical array record is used and the reduced record of the array from yours?

The array syntax makes no difference and isn't the point. The point is the use of form_multiselect instead fo form_dropdown.

Creating an array using the array() language construct, e.g.
PHP Code:
array('1' => 'First''2' => 'Second''3' => 'third''4' => 'fourth',) 
or using the short syntax, e.g.
PHP Code:
['1' => 'First''2' => 'Second''3' => 'third''4' => 'fourth',] 
produces the exact same array, the only difference is the code style..

I'm just in the habit of using the short syntax because I like typing less to get the same results. Sorry if that caused confusion.


RE: dropdown with multiple - Nome - 07-29-2018

(07-27-2018, 05:20 AM)dave friend Wrote: The array syntax makes no difference and isn't the point. The point is the use of form_multiselect instead fo form_dropdown.

No, I'm sorry I did not notice that there is form_multiselect .  You are right =)