• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with multiple select boxes

#1
[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.

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

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

#4
[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?

#5
[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.

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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.