如何选中复选框?

如何选中复选框?

问题描述:

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

在我的应用程序的CMS中,我有一个编辑项目表单。 p>

用户将要编辑的项目有一些复选框。 p>

其中一些项目在创建时被选中,并且该选择已存储在我的数据库中,因为它会影响项目在实际站点中的显示方式。 p>

假设 $ checked code>是一个布尔变量,如果创建该项目的用户选择了该框,则该属性为true,我的代码是什么以显示所选复选框 if($ checked) code>? p> div>

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':'') ?> />