CodeIgniter Forums
Help with multiple select boxes - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Help with multiple select boxes (/thread-11057.html)



Help with multiple select boxes - El Forum - 08-24-2008

[eluser]dobbler[/eluser]
I have a multiple select box that I'm trying to get the values of on form submission. All I'm getting is the last one.

I've tried:

for ($i=1; $i<=count($this->input->post('appbox2')); $i++) {
echo "ID: " . $this->input->post('appbox2') . "<br />";
}

Just to see what's being sent but it's only showing the last value in the box.

I'm sure this is simple but it's driving me nuts..

Thanks.


Help with multiple select boxes - El Forum - 08-24-2008

[eluser]steelaz[/eluser]
What's the name of your form field? It should look something like 'appbox2[]'.


Help with multiple select boxes - El Forum - 08-24-2008

[eluser]dobbler[/eluser]
Thanks steelaz, I just got it.. it needed to be an array.. Duh!


Help with multiple select boxes - El Forum - 09-04-2008

[eluser]CarNinja[/eluser]
[quote author="steelaz" date="1219647711"]What's the name of your form field? It should look something like 'appbox2[]'.[/quote]

I'm having this same problem, but this solution doesn't really make sense to me. I've even tried print_r( $_POST ); and all I get is the last value. Can anyone shed further light on this?


Help with multiple select boxes - El Forum - 09-04-2008

[eluser]dobbler[/eluser]
Hi CarNinja,

This didn't make any sense to me either. The form fields should look like this:

&lt;INPUT type="CHECKBOX" name="itemtype[]" value="1" /&gt;&lt;br />
&lt;INPUT type="CHECKBOX" name="itemtype[]" value="2" /&gt;&lt;br />
&lt;INPUT type="CHECKBOX" name="itemtype[]" value="3" /&gt;&lt;br />

Then try print_r($_POST); and you should get an array of the values.. They have to be checked though.

The name of the input being an array threw me, I came from an ASP background so this was confusing but it worked so I'm not asking any questions!

Hope this helps, it was driving me mental so I feel your pain..

Rob.


Help with multiple select boxes - El Forum - 09-04-2008

[eluser]CarNinja[/eluser]
[quote author="dobbler" date="1220533060"]Hi CarNinja,

This didn't make any sense to me either. The form fields should look like this:

&lt;INPUT type="CHECKBOX" name="itemtype[]" value="1" /&gt;&lt;br />
&lt;INPUT type="CHECKBOX" name="itemtype[]" value="2" /&gt;&lt;br />
&lt;INPUT type="CHECKBOX" name="itemtype[]" value="3" /&gt;&lt;br />

Then try print_r($_POST); and you should get an array of the values.. They have to be checked though.

The name of the input being an array threw me, I came from an ASP background so this was confusing but it worked so I'm not asking any questions!

Hope this helps, it was driving me mental so I feel your pain..

Rob.[/quote]

Thanks Rob,

I actually found the solution a few moments ago. I'll post it here for future reference:

When adding the field to the page, it needs the brackets, as you've pointed out.

Code:
&lt;?= form_dropdown( 'ad_states[]', $states, $this->input->post( 'ad_states' ), 'id="ad_states" multiple="multiple" tabindex="8"' )?&gt;


However, when you access the variable, you drop the brackets:

Code:
$this->input->post( 'ad_states' )


I hope this clears anything up for people searching in the future. Smile