从复选框值PHP创建一个数组

时间:2012-06-26 22:54:45

标签: php

我有很多复选框。我想将它们的值拉入逗号分隔的数组中。 如果复选框被取消,则值为空,所以:

巴,停车,,,,,电视等

我该怎么做?在制作数组后,我将提交到数据库。

    <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>

5 个答案:

答案 0 :(得分:1)

/* 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 :(得分:0)

将字段命名为checkbox [],然后在PHP中,您可以使用$ _GET ['复选框']获得类似的数组。

答案 2 :(得分:0)

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

答案 3 :(得分:0)

你应该为名字字段赋予它们所有相同的名称,当你获取名称时,它将返回所有那些被检查的值的数组。

以下是您可以阅读的参考资料: http://www.html-form-guide.com/php-form/php-form-checkbox.html

答案 4 :(得分:0)

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

<强> HTML

<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

// 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;