Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home

General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2

Drivers
Driver Scan
Driver Forum

Software
Automatic Updates

BIOS Updates

My Computing.Net

Solution Center

Free IT eBook

Howtos

Site Search

Message Find

RSS Feeds

Install Guides

Data Recovery

About

Home
Reply to Message Icon Go to Main Page Icon

PHP & checkboxes

Original Message
Name: Don Arnett
Date: November 16, 2004 at 20:27:42 Pacific
Subject: PHP & checkboxes
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


Report Offensive Message For Removal


Response Number 1
Name: Khalid
Date: November 16, 2004 at 23:12:14 Pacific
Subject: PHP & checkboxes
Reply: (edit)
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!



Report Offensive Follow Up For Removal

Response Number 2
Name: Don Arnett
Date: November 17, 2004 at 06:41:04 Pacific
Subject: PHP & checkboxes
Reply: (edit)
Thanks Khalid - worked like a charm



Report Offensive Follow Up For Removal




Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: PHP & checkboxes

Comments:

 
  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 


Data Recovery Software




acer 312T BIOS problem

K7 Turbo possible max fsb?

Pc anywher problem

WinFLP & OE/Outlook2003

Computer resets after a few minutes


The information on Computing.Net is the opinions of its users. Such opinions may not be accurate and they are to be used at your own risk. Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE

All content ©1996-2007 Computing.Net, LLC