|
Posted by Tom Rogers on 11/26/08 11:17
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=' . $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' id='interesses'
value=Alojamento'>><br />
MG> Artesanato<input name='interesses' type='checkbox' id='interesses'
value=Artesanato'>><br />
MG> Eventos<input name='interesses' type='checkbox' 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
[Back to original message]
|