how to make a checkbox selected?

10,202

Solution 1

echo '<input type="checkbox" value="123" '.($checkedValue == 123 ? 'checked="checked"':'').' />';

Solution 2

if($checked){
echo 'checked="checked"';
}

Solution 3

You need to add an attribute checked="checked" to your input element. (Or simply checked depending on your doctype.)

Solution 4

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\"";
Share:
10,202

Related videos on Youtube

ppp
Author by

ppp

Updated on June 04, 2022

Comments

  • ppp
    ppp about 2 years

    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) ?

    • andrewk
      andrewk over 12 years
      Did you test if($checked) and failed????