首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于MySQL数据呈现HTML复选框输入

问基于MySQL数据呈现HTML复选框输入
EN

Stack Overflow用户
提问于 2009-01-14 09:58:51
回答 2查看 139关注 0票数 0

我有这个表包含名称和值,我怎样才能将内容转换成

代码语言:javascript
复制
$row=mysql_fetch_array($result);

添加到复选框中?

EN

回答 2

Stack Overflow用户

发布于 2009-01-14 10:34:48

假设$row是一个将列名映射到布尔值1或0的关联数组,您可以这样做:

代码语言:javascript
复制
foreach($row as $colname=>$boolean)
{
    //create a name for the checkbox which will produce a nice 
    //PHP array of checked column names in $_POST['col']
    $name="col[$colname]";

    //create an id for the checkbox
    $id='col'.$colname;

    //now we output the checkbox - on form submission you will
    //see an element in $_POST['col'][$colname] if checked, and
    //no element at all if unchecked...
    echo '<input type="checkbox" value="1" '.
       'name="'.$name.'" id="'.$id.'" '.
       ($boolean?'checked="checked"':'').
       '>';

    //output a label - note we'd tied this to the id of the checkbox
    //which means you can click the label to tick the box   
    echo "<label for=\"$id\">colname</label><br/>";
}

当表单提交时,您将在$_POST‘’col‘中获得一个按列名索引的数组,但仅限于选中的那些框,因此您将所有未设置的列设置为false。

票数 3
EN

Stack Overflow用户

发布于 2009-01-14 10:04:42

查看此处:http://dev.w3.org/html5/spec/Overview.html#checkbox-state

所以,试试这个:

代码语言:javascript
复制
<input type="checkbox" checked="true">
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/442460

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档