PHP 复选框设置为根据数据库值进行检查

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16239663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:45:17  来源:igfitidea点击:

PHP checkbox set to check based on database value

phphtmlmysqlcheckbox

提问by snookian

I have a system where people fill in their information and later can go back and edit certain parts, basically the enter personal info and check whether they want to know extra info, these bits of extra infos are checkboxes, 4 of them. the user will select up to any of the 4 and the database has 4 fields set to no, if they select one it changes to yes. I want them to be able to go back and deselect or reselect any of these 4 checkboxes, so what i want s for the checkboxes to be selected if the values is yes and unselected if the value is not.

我有一个系统,人们填写他们的信息,然后可以返回并编辑某些部分,基本上是输入个人信息并检查他们是否想知道额外的信息,这些额外的信息是复选框,其中有 4 个。用户最多可以选择 4 个中的任何一个,并且数据库有 4 个字段设置为 no,如果他们选择一个,则更改为 yes。我希望他们能够返回并取消选择或重新选择这 4 个复选框中的任何一个,所以如果值为是,我希望复选框被选中,如果值不是,则取消选中。

the fields are tag_1, tag_2, tag_3, tag_4

字段是 tag_1、tag_2、tag_3、tag_4

Anyhelp greatly appreciated

Anyhelp非常感谢

I gather some kind of if statement but not sure how to involve it in a checkbox.

我收集了某种 if 语句,但不确定如何将其包含在复选框中。

<label for="tag_1">Tag 1</label>
<input type="checkbox" name="tag_1" id="tag_1" value="yes" />

Ian

伊恩

回答by DaveyBoy

Extract the information from the database for the checkbox fields. Next change the above example line to: (this code assumes that you've retrieved the information for the user into an associative array called dbvalueand the DB field names match those on the HTML form)

从数据库中提取复选框字段的信息。接下来将上面的示例行更改为:(此代码假定您已将用户的信息检索到名为的关联数组中dbvalue,并且 DB 字段名称与 HTML 表单上的名称匹配)

<input type="checkbox" name="tag_1" id="tag_1" value="yes" <?php echo ($dbvalue['tag_1']==1 ? 'checked' : '');?>>

If you're looking for the code to do everything for you, you've come to the wrong place.

如果您正在寻找代码来为您做所有事情,那么您来错地方了。

回答by Damien

Add this code inside your input tag

将此代码添加到您的输入标签中

<?php if ($tag_1 == 'yes') echo "checked='checked'"; ?>

回答by Dan

This simplest ways is to add the "checked attribute.

这个最简单的方法是添加“checked”属性。

<label for="tag_1">Tag 1</label>
<input type="checkbox" name="tag_1" id="tag_1" value="yes" 
    <?php if($tag_1_saved_value === 'yes') echo 'checked="checked"';?> />

回答by Shriganesh Shintre

You can read database value in to a variable and then set the variable as follows

您可以将数据库值读入变量,然后按如下方式设置变量

$app_container->assign('checked_flag', $db_data=='0'  ? '' : 'checked');

And in html you can just use the checked_flag variable as follows

而在 html 中,您可以使用 checked_flag 变量如下

<input type="checkbox" id="chk_test" name="chk_test" value="1" {checked_flag}>

回答by NicoDeug

Use checked="checked"attribute if you want your checkbox to be checked.

checked="checked"如果您希望选中复选框,请使用属性。