PHP 复选框设置为根据数据库值进行检查
我有一个系统,人们填写他们的信息,然后可以返回并编辑某些部分,基本上是输入个人信息并检查他们是否想知道额外信息,这些额外信息是复选框,其中 4 个.用户最多可以选择 4 个中的任何一个,并且数据库有 4 个字段设置为 no,如果他们选择一个,则更改为 yes.我希望他们能够返回并取消选择或重新选择这 4 个复选框中的任何一个,所以如果值为是,我希望复选框被选中,如果值不是,则取消选中.
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.
字段是 tag_1, tag_2, tag_3, tag_4
the fields are tag_1, tag_2, tag_3, tag_4
非常感谢任何帮助
我收集了某种 if 语句,但不确定如何将其包含在复选框中.
I gather some kind of if statement but not sure how to involve it in a checkbox.
<label for="tag_1">Tag 1</label>
<input type="checkbox" name="tag_1" id="tag_1" value="yes" />
伊恩
推荐答案
从数据库中提取复选框字段的信息.接下来将上面的示例行更改为:(此代码假定您已将用户信息检索到名为 dbvalue
的关联数组中,并且 DB 字段名称与 HTML 表单上的名称匹配)
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 dbvalue
and the DB field names match those on the HTML form)
<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.
相关文章