我有很多复选框。我想将它们的值放入一个逗号分隔的数组中。如果取消选中复选框,则值将为空,因此:
酒吧、停车场、电视等
我该怎么做呢?在生成数组后,我将提交到一个数据库中。
<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>发布于 2012-06-27 07:06:32
如果可能,我建议您在变量名中使用[]来传递数组并在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;https://stackoverflow.com/questions/11217080
复制相似问题