PHP复选框设置为基于数据库值进行检查 [英] PHP checkbox set to check based on database value

查看:224
本文介绍了PHP复选框设置为基于数据库值进行检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个系统,人们填写他们的信息,以后可以回去和编辑某些部分,基本上输入个人信息,并检查他们是否想知道额外的信息,这些位的额外的信息是复选框,其中4。用户将选择最多4个,数据库有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

Anyhelp非常感谢

Anyhelp greatly appreciated

我收集了一些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" />

Ian

推荐答案

从复选框字段的数据库中提取信息。接下来将上面的示例行更改为:
(此代码假定您已将用户的信息检索到一个名为 dbvalue 的关联数组中,并且数据库字段名称匹配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.

这篇关于PHP复选框设置为基于数据库值进行检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆