我有很多复选框。我想将它们的值放入一个逗号分隔的数组中。如果取消选中复选框,则值将为空,因此:
酒吧、停车场、电视等
我该怎么做呢?在生成数组后,我将提交到一个数据库中。
<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 06:59:09
/* 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-27 06:58:04
将你的字段命名为checkboxes[],然后在PHP中你可以得到一个类似的数组,带有$_ get‘’checkboxes‘。
发布于 2012-06-27 06:58:29
对我来说,听起来像是ajax的工作。我会考虑使用dojo或jquery来收集和传递数据。
https://stackoverflow.com/questions/11217080
复制相似问题