首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用checkbox值创建数组

使用checkbox值创建数组
EN

Stack Overflow用户
提问于 2012-06-27 06:54:45
回答 5查看 637关注 0票数 0

我有很多复选框。我想将它们的值放入一个逗号分隔的数组中。如果取消选中复选框,则值将为空,因此:

酒吧、停车场、电视等

我该怎么做呢?在生成数组后,我将提交到一个数据库中。

代码语言:javascript
运行
复制
    <p>
        <label for="meta_box_check_bar">bar</label>
        <input type="checkbox" id="meta_box_check_bar" name="meta_box_check_bar" value="bar" />

        <label for="meta_box_check_parking">parking</label>
        <input type="checkbox" id="meta_box_check_parking" name="meta_box_check_parking" value="parking" />

        <label for="">accessible-for-disabled</label>
        <input type="checkbox" id="meta_box_check_accessible-for-disabled" name="meta_box_check_accessible-for-disabled" value="accessible-for-disabled" />

        <label for="">air-conditioning</label>
        <input type="checkbox" id="meta_box_check_air-conditioning" name="meta_box_check_air-conditioning" value="air-conditioning" />

        <label for="">frigobar </label>
        <input type="checkbox" id="meta_box_check_frigobar" name="meta_box_check_frigobar" value="frigobar" />

        <label for="">pets</label>
        <input type="checkbox" id="meta_box_check_pets" name="meta_box_check_pets" value="pets" />

        <label for="">phone</label>
        <input type="checkbox" id="meta_box_check_phone" name="meta_box_check_phone" value="phone" />

        <label for="">tv</label>
        <input type="checkbox" id="meta_box_check_tv" name="meta_box_check_tv" value="tv" />

        <label for="">typical-local-dishes</label>
        <input type="checkbox" id="meta_box_check_typical-local-dishes" name="meta_box_check_typical-local-dishes" value="typical-local-dishes" />
    </p>
EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2012-06-27 06:59:09

代码语言:javascript
运行
复制
/* make an array for all used checkbox */
$used_checkboxes = array();

/* make an array whit all options */
$avaible_checkboxes = explode(',', "bar,parking,accessible-for-disabled,air-conditioning,frigobar,pets,phone,tv,typical-local-dishes");


/* loop troguht all avaible checkboxes */
foreach($avaible_checkboxes as $current_key)
{
   /* check if the checkbox was sent */
   if(isset($_POST["meta_box_check_{$current_key}"]))
   {
      /* if sent, add key to list */
      $used_checkboxes[$current_key] = $current_key;
   }
   else
   {
      /* if not sent, add empty value to list */
      $used_checkboxes[$current_key] = '';
   }
}

/* convert list to csv */
$used_checkboxes_csv = implode(',', $used_checkboxes);
票数 1
EN

Stack Overflow用户

发布于 2012-06-27 06:58:04

将你的字段命名为checkboxes[],然后在PHP中你可以得到一个类似的数组,带有$_ get‘’checkboxes‘。

票数 0
EN

Stack Overflow用户

发布于 2012-06-27 06:58:29

对我来说,听起来像是ajax的工作。我会考虑使用dojo或jquery来收集和传递数据。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11217080

复制
相关文章

相似问题

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