Re: Dummy question about knowing marked checkboes

From: Date: Sun, 29 May 2005 09:15:08 +0000
Subject: Re: Dummy question about knowing marked checkboes
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-215958@lists.php.net to get a copy of this message
Hi, Sunday, May 29, 2005, 5:50:13 PM, you wrote: MG> Hi, MG> First of all, my apologies to all list members for this dummy question. MG> I've made my home work before posting here, but the truth is that i MG> didn't found an answer for such a simple question. MG> Only examples of more sophisticated related things. MG> I have this piece of code that produces various checkboxes inside a form: MG> ------------------------------------------------------------------- MG> // select interesses names and values to build the chekboxes MG> $recordSet = &$conn->Execute('SELECT interesses FROM interesses ORDER BY MG> interesses ASC'); MG> // build the checkboxe's list MG> if (!$recordSet) MG> print $conn->ErrorMsg(); MG> else { MG> while (!$recordSet->EOF) { MG> print($recordSet->fields[0] . '<input name=\'interesses\' MG> type=\'checkbox\' id=\'interesses\' value=' . MG> $recordSet->fields[0] . '\'>>'); MG> echo"<br />"; $recordSet->>MoveNext(); MG> } $recordSet->>Close(); $conn->>Close(); MG> } MG> --------------------------------------------------------------------- MG> Here's the correspondent HTML output: MG> --------------------------------------------------------------------- MG> Alojamento<input name='interesses' type='checkbox' MG> id='interesses' value=Alojamento'>><br /> MG> Artesanato<input name='interesses' type='checkbox' MG> id='interesses' value=Artesanato'>><br /> MG> Eventos<input name='interesses' type='checkbox' MG> id='interesses' value=Eventos'>> MG> (etc.) MG> --------------------------------------------------------------------- MG> My dummy question, for which i apologise once more is: after submitting MG> the form, how can i know which chekboxes were checked ? MG> Thanking you in advance. MG> Warm Regards, MG> Mário Gamito Make them an array by adding [] at the end of the name. Also you are missing a quote at the start of the value= (double quotes makes it easier to print in my opinion) It should be like this: while (!$recordSet->EOF) { print($recordSet->fields[0] . '<input name="interesses[]" type="checkbox" id="interesses" value="' . $recordSet->fields[0].'">' ); Then any checkboxes checked will be in an array called interesses $interesses = (isset($_POST['interesses']))? $_POST['interesses'] : array(); if(count($interesses) > 0){ //checkboxes have been ticked }else{ //none ticked } -- regards, Tom

« previous php.general (#215958) next »