You are here: Re: [PHP] Dummy question about knowing marked checkboes « PHP « IT news, forums, messages
Re: [PHP] Dummy question about knowing marked checkboes

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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация