Reputation: 10913
How do I get to get the values of checkboxes from mysql database, for example if stat6 is dependent, then when I try to fetch the corresponding record using a primary key. Then if the stat6 is checked when I first added the record. It must also be checked when I update. But its not the case. How do I fix this?
<td></td>
<input type='hidden' name="stats6" value="0">
<td><input name="stats6" type="checkbox" id="dep" value="<?php echo $row["STAT6"]; ?>">Dependent</td>
<td><font size="3"></td>
<td></td>
<input type='hidden' name="stats7" value="0">
<td><input name="stats7" type="checkbox" id="emp" value="<?php echo $row["STAT7"]; ?>">Employee</td>
Upvotes: 1
Views: 2345
Reputation: 300
You have to add the attribute "checked", if the box should be checked:
<td><input name="stats6" type="checkbox" id="dep" value="<?php echo $row["STAT6"]; ?>" <? if($row["STAT6"]){ echo "checked"; } ?>>Dependent</td>
Upvotes: 0
Reputation: 834
The easiest way to do this would be something like this:
<input name="stats6" type="checkbox" id="dep" <?php echo $row["STAT6"] ? "checked": ""; ?>>
Upvotes: 2
Reputation: 30035
<td></td>
<td><input name="stats6" type="checkbox" id="dep" value="<?php echo $row["STAT6"]; ?>" <?php echo $row["STAT6"] ? 'checked="checked"' : ''; ?> >Dependent</td>
<td><font size="3"></td>
<td></td>
<td><input name="stats7" type="checkbox" id="emp" value="<?php echo $row["STAT7"]; ?>" <?php echo $row["STAT7"] ? 'checked="checked"' : ''; ?>>Employee</td>
Upvotes: 3