首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
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

Stack Overflow用户

发布于 2012-06-27 07:06:32

如果可能,我建议您在变量名中使用[]来传递数组并在PHP中检索它们的值。如下所示:

HTML

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

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

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

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

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

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

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

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

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

PHP

代码语言:javascript
运行
复制
// Prevent errors when nothing is checked
if( ! is_array($_POST['array']) )
    $_POST['array'] = array();

// Possible items
$possible_item[] = "bar";
$possible_item[] = "parking";
$possible_item[] = "accessible-for-disabled";
$possible_item[] = "air-conditioning";
$possible_item[] = "frigobar";
$possible_item[] = "pets";
$possible_item[] = "phone";
$possible_item[] = "tv";
$possible_item[] = "typical-local-dishes";

// Starting output string
$output = '';

// Loop the possible items
foreach($possible_item as $value)
{
    // If item is in POST array, add to the output string, else put just comma if needed
    if( in_array($value, $_POST['array']) )
        $output .=  ( empty($output) ? '' : ',' ) . $value;
    else
        $output .=  ( empty($output) ? '' : ',' );
}

echo $output;
票数 0
EN
查看全部 5 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11217080

复制
相关文章

相似问题

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