Computing.Net > Forums > Web Development > PHP & checkboxes

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

PHP & checkboxes

Reply to Message Icon

Name: Don Arnett
Date: November 16, 2004 at 20:27:42 Pacific
OS: na
CPU/Ram: na
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: Khalid
Date: November 16, 2004 at 23:12:14 Pacific
Reply:

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!



0

Response Number 2
Name: Don Arnett
Date: November 17, 2004 at 06:41:04 Pacific
Reply:

Thanks Khalid - worked like a charm



0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Web Development Forum Home


Sponsored links

Ads by Google


Results for: PHP & checkboxes

Help with Checkboxes and array PHP www.computing.net/answers/webdevel/help-with-checkboxes-and-array-php/2568.html

php checkboxes www.computing.net/answers/webdevel/php-checkboxes/2390.html

PHP/MYSQL updating w/checkboxes www.computing.net/answers/webdevel/phpmysql-updating-wcheckboxes/3321.html