user2148024 user2148024 - 6 months ago 16
HTML Question

Check checkboxes based on database values

I have values in a database, like dates and checkbox values. I want to check boxes which have dates in the database; other checkboxes should be empty. How to have the correct checkboxes (out of 30) checked?.

<?php
for($i = 1; $i <= $no_of_days; $i++) {
if($i == 15 || $i == 18) {
?>
<div style="width:300px;float:left;">
<input type="checkbox" value="1" name="isactive" checked="checked" />
<?php echo $start = date(''.$i.'-m-Y'); ?>
</br></br></br>
</div>
<?php
}
}
?>

Answer

This is what you need :

<?php 
    for($i=1;$i<=20;$i++) {
        if($i ==15 || $i== 18) {
            $is_checked = 'checked';
        } ?>

        <div style = "width : 300px; float : left;" >
            <input type = "checkbox" value = "1" name = "isactive"
                <?php echo $is_checked; ?>
                <?php
                   if( @$isActive[$i] == 1 ) { ?> 
                       checked = "checked" <?php
                   } ?> 
            />

            <?php echo $start = date(''.$i.'-m-Y'); ?>

            </br></br></br>     
        </div> <?php
    } ?>

Give it a try

Comments