Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

validating checkboxes

Status
Not open for further replies.

cgillin

Programmer
Joined
Mar 20, 2003
Messages
58
Location
US
In a form I have an an array of checkboxes with each one reading something like..

<input type="checkbox" name="states[]" value="PA">

My problem is I want to do form validation but not sure how to check if each checkbox has been checked or not. Does anyone know of either a way to use javascript to validate an array of checkboxes declared as above or of another way to pass the arry into PHP other than declaring ...

name="states[]"

Thanks for your help
Chris
 
Sorry, should have said that before.

What I want to do through javascript is be able to determine which boxes have been checked and also have an option to select all checkboxes in the array.

Thanks
Chris
 
Checking all checkboxes -- this is a JavaScript question and as such is not in the scope of this forum. My one helpful hint here to point out that you can use the "id" attribute of a form field to reference that field, just as you can the "name" attribute. Thus, you can have a field defined as:

<input type="text" name="foo[1]" id="foo_1">

and make that field available to JavaScript and to PHP as an array element.

In terms of PHP, when the script is submitted, only those checkboxes checked will have values submitted.



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top