In my application's CMS, I have an edit items form.
The items users will be editing have some checkboxes.
Some of them were selected upon creation, and that selection has been stored in my database, because it affects how items are displayed in the actual site.
Supposing $checked
is a boolean variable that is true if the user that created the item had selected the box, what would be my code to show a selected checkbox if($checked)
?
echo '<input type="checkbox" value="123" '.($checkedValue == 123 ? 'checked="checked"':'').' />';
if($checked){
echo 'checked="checked"';
}
The code to show a checkbox as selected from the very beginning is something like this:
<checkbox name="bla" selected="selected" />
Which would render as a already selected checkbox. This combined with an if would create something among these lines:
if($checked)
$tag_info .= "selected=\"selected\"";
You need to add an attribute checked="checked" to your input element. (Or simply checked depending on your doctype.)
if your $checked returns TRUE or FALSE then use
<input type="checkbox" name="" value="" id="" class="" <?php echo (($checked)?'checked':'') ?> />