Saturday, 18 May 2013 13:56

how to create dynamic checkboxes with checked state

Written by 
Rate this item
(0 votes)

the following codes show how to create dynamic
check boxes according to database data.also the
tricky part is that whether a check box will
be checked or not can be done by the code '$checked'.

here breedsid array is made before.so the id's in
the breedsid array will be checked.codes shown -

<?php

$breedlist = array();

$q = mysql_query("SELECT id,breed,code FROM breeds
ORDER BY breed");
$total = mysql_num_rows($q);

while($i = mysql_fetch_array($q))
{
$checked = "";
$breedid2 = $i["id"];
$breed2 = $i["breed"];
for($k = 0; $k < count($breedsid); $k++)
{
if($breedsid[$k]==$breedid2){$checked = "CHECKED";}
}
echo ' <tr>
<td valign="top" class="bodymediumblk" align="left">
<input type="checkbox" name="checkbox[]"
value= "'.$breedid2.'" '.$checked.'>'.$breed2.'
</td></tr>';}?>

Read 1249 times
Super User

Email This email address is being protected from spambots. You need JavaScript enabled to view it.
Login to post comments