Page 1 of 1

Using Multiple CheckBoxes in a Table

Posted: Thu Jun 13, 2002 9:01 pm
by dickey
I wish to present a <form> to users containing a <table> where each <tr> contains the following product details:

id, description, price, and a checkbox against each row.
the form has a submit button

I am able to do this much. (code at bottom)

In respect of the form and its action, how do I make the checkbox in each row correspond to the correct record to perform the action on.

Do I add the record id to the name of the checkbox so that a variable exists once the form is submitted.

Any assistance would be greatly appreciated.

- Andrew

CODE:
<?
$query = "SELECT id, description, price FROM products";
$result = mysql_query($query);
$num_results = mysql_num_rows($result);

if ($num_results ==0)
{
echo "No matching items found";
exit;
}
?>

available products<BR><BR>
<FORM method="POST" action="prodfindstep5.php">
<TABLE>

<?
while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
{
print "\t<TR>\n";
foreach ($line as $col_value)
{
print "\t\t<TD>$col_value</TD>\n";
}
?>
<TD><INPUT type="checkbox"></TD>
<?
print "\t</tr>\n";
}
?>
<TR><TD align="center" colspan="4"><INPUT type="submit" value="more info"></TD></TR>
</TABLE>
</FORM>
<?

}
?>

Posted: Thu Jun 13, 2002 9:07 pm
by Peter
One way you could do it, is name the checkboxes mr_array[] or whatever and then set the value of the individual checkboxes to their ID numbers...

then you can go...

foreach($mr_array as $selection){
// do stuff
echo $selection;
}

Re: Using Multiple CheckBoxes in a Table

Posted: Thu Jun 13, 2002 11:10 pm
by roninblade
dickey wrote:In respect of the form and its action, how do I make the checkbox in each row correspond to the correct record to perform the action on.

Do I add the record id to the name of the checkbox so that a variable exists once the form is submitted.
yes add the record id, since it is the unique value of the record.

i also agree with Peter, you should name the checkboxes as an array so you wont have any difficulty accessing the values later.

Posted: Fri Jun 14, 2002 1:49 am
by twigletmac
Following those ideas you should end up with something like:

Code: Select all

echo '<input type="checkbox" name="info&#1111;]" value="'.$id.'" />';
Mac

Using Multiple CheckBoxes in a Table

Posted: Fri Jun 14, 2002 7:06 am
by dickey
Thanks very much for your assistance Peter, Roninblade, and Twigletmac.

Named the checkboxes as an array as you suggested.

The array is only populated by those checkboxes that were checked, which makes processing very easy.

Thanks again.