0

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

酒吧,停车场,,,,,电视等

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

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

5 回答 5

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);
于 2012-06-26T22:59:09.247 回答
0

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

于 2012-06-26T22:58:04.067 回答
0

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

于 2012-06-26T22:58:29.077 回答
0

您应该为名称字段赋予它们所有相同的名称,这样当您获取名称时,它将返回所有已检查值的数组。

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

于 2012-06-26T22:59:05.380 回答
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;
于 2012-06-26T23:06:32.273 回答