Re: Dummy question about knowing marked checkboes

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux