RE: [PHP] Couple of questions

From: Date: Thu, 24 Oct 2002 18:12:05 +0000
Subject: RE: [PHP] Couple of questions
Groups: php.general 
Request: Send a blank email to php-general+get-121326@lists.php.net to get a copy of this message
> -----Original Message----- > From: John Nichel [mailto:jnichel@by-tor.com] > Sent: 24 October 2002 18:46 > > 2) I'm trying to validate a checkbox element in a form. Due to using > php to process this form, the checkbox element must have a name ending > in square brackets... > > <input type="checkbox" value="somevalue" name="somename[]" > /> > > The problem I'm having is trying to loop thru this element to > see if at > least one of the boxes are checked.... > > for ( i = 0; i < > document.forms[0].elements['somename[]'].length; i++ ) > { > alert ( document.forms[0].elements['somename[]'][i].checked ); > } The elements property of a form is an array indexed by integers. If an element has a NAME value, that name is also a property of the *form*, not the form's elements array, so the correct syntax is: for ( i = 0; i < document.forms[0]['somename[]'].length; i++ ) { alert ( document.forms[0]['somename[]'][i].checked ); } Cheers! Mike --------------------------------------------------------------------- Mike Ford, Electronic Information Services Adviser, Learning Support Services, Learning & Information Services, JG125, James Graham Building, Leeds Metropolitan University, Beckett Park, LEEDS, LS6 3QS, United Kingdom Email: m.ford@lmu.ac.uk Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211

« previous php.general (#121326) next »