Don't name the checkboxes all the same, instead:

Option1 <input type="checkbox" name="ck0"><br>
Option2 <input type="checkbox" name="ck1"><br>
Option3 <input type="checkbox" name="ck2"><br>

Then handle it with

function getCheckboxResults($prefix=FALSE)
    global $_POST ;
    $results = NULL ; // needed for 'return' in case no items (v4.1.x)
            if(isset($_POST[$prefix.$i])) $results[] = $i ;
    } else { return -1 ; } // throw exception in script
    return $results ;

$selectedCKItems = getCheckboxResults('ck');
if(!$selectedCKItems) makeError('Please select at least one item.');

If a checkbox is not selected, the CGI will drop it (as in, not pass on the
name/value pair - meaning the name is not there - which is why you test for
it's existance).

I didn't test that example. But if it works, the function might be a nice
addition to your (and mine) library :)

you might use it like to:
$prefix = 'ck';
Forech($arrLabels AS $i=>$label)
printf('%s <input type="checkbox" name="%s%d"><br>\n',$label,$prefix,$i);

Then put this member functions in a class deinition and register instatiated
objects with a session to use them across pages. $arrLabels would be a
property of the class. 'ck' would be the value of another property. Maybe
your foreach statement was in a member function.

OK, I'll shut up now :)


> -----Original Message-----
> From: Erik Price [mailto:[EMAIL PROTECTED]] 
> Sent: Tuesday, 5 March 2002 2:24 AM
> To: bvr
> Cc: Ben Turner; [EMAIL PROTECTED]
> Subject: Re: [PHP] CheckBoxes....
> On Sunday, March 3, 2002, at 11:15  AM, bvr wrote:
> > If you append '[]' to the name, an array will be returned 
> after submit
> > with the values of the checked checkboxes.
> >
> Is there any other way of doing this?  Using the brackets at 
> the end of 
> the input's name is super-convenient, making it relatively 
> easy to work 
> with arrays and user input in PHP.  I love that.  But I also wish I 
> could validate my pages as XHTML Strict.  I know it's not 
> really a big 
> deal, it's just a pride-in-work thing.
> Alternatively, is there any plans to change the DTD for XHTML 
> Strict to 
> allow brackets?  IMO it's not something that cause problems 
> if it were 
> to be supported.  Kind of lame that it's not, actually.
> Until then, I'll keep using them (brackets) b/c there's some 
> things that 
> it seems I can't do without this technique, but it would be nice....
> Erik
> ----
> Erik Price
> Web Developer Temp
> Media Lab, H.H. Brown
> -- 
> PHP General Mailing List (
> To unsubscribe, visit:

PHP General Mailing List (
To unsubscribe, visit:

Reply via email to