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


make an array from checkbox values 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>
/* 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);

将字段命名为 checkbox[],然后在 PHP 中你可以得到一个类似的数组,其中包含 $_GET['checkboxes']。

对我来说听起来

像是ajax的工作。我会考虑 dojo 或 jquery 来收集和传递数据。

您应该

为名称字段指定所有相同的名称,这样当您执行名称获取时,它将返回所有检查值的数组。

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

如果

可能的话,我建议您在变量名称中使用[]来传递数组并在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;