Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I'm new to PHP and have been away from HTML for a while. I'm trying to learn some PHP as I build a fairly small set of pages. I've used JSP before so am familiar PHP's style. I'm using my PHP book as a reference even tho I probably should just sit down and read it.
I have a PHP page with a FORM that contains a bunch of checkboxes. The name of all the checkboxes is 'state'. The values are 2 letter state codes (NE, IA, MO, etc).
When I submit the form, I want to build a list of all of the states that were checked.
When I print the value of $state, I get one state, probably the last one checked. I was hoping to get a comma separated list.
I realized that I could name each checkbox different, but then I'd have to run thru the list of all 40 - 50 states to see which were checked.
Is there a better way?
Thanks

Remember that checkboxes with the same name work in a group, and the form will pass their value as an array. However, in your form you should tell explicitly that the checkboxes should pass their value as an array.
For example:
<input type="checkbox" name="state[]" value="NE">
<input type="checkbox" name="state[]" value="IA">
<input type="checkbox" name="state[]" value="MO">So in your case your script should loop through all the elements in the area to see if the box was checked or not.
For example:
<?php
$state=$_POST['state'];
foreach ($state as $statename)
{
echo "$statename is checked";
}?>
Good luck!

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |