php处理checkbox
2012-01-26 15:09 youxin 阅读(395) 评论(0) 编辑 收藏 举报<input type=”checkbox” id=”imagesonly” name=”imagesonly” value=”yes” />
<label for=”imagesonly”> Check here if you only want images.</label>
<?php
if (isset($_GET[‘imagesonly’]) && $_GET[‘imagesonly’] == ‘Yes’) {
$imagesonly = ‘Yes‘;
} else {
$imagesonly = ‘No‘;
}
echo ‘Images Only? ‘ . $imagesonly . ‘<br />’;
?>
<fieldset>
<legend>where do you live?</legend>
<input type="checkbox" id="arearural" name="areatypes[]" value="rural"/>
<label for="arearural">Rural </label>
<input type="checkbox" id="areasuburb" name="areatype[]" value="suburb"/>
<label for="areasuburb">Suburb</label>
<input type="checkbox" id="areacity" name="areatypes[]" value="city"/>
<label for="areacity">City </label>
</fieldset>
In the example you also had a group of checkboxes that you want to process as an array. You signifi
ed this by using the same name and suffi xing the name with square brackets. Because the checkboxes
are sent only if they are checked, you use isset() to see if any of the areatype checkboxes
were selected at all. Then you loop through and fi lter each element of the array and build the fi ltered
$areatypes array that you will display. See Figure 11-7 for the results.
if (isset($_GET[‘areatypes’])) {
$inputs = array();
$inputs = $_GET[‘areatypes’];
foreach ($inputs as $input) {
$areatypes[] = filter_var($input, FILTER_SANITIZE_STRING);
}
foreach ($areatypes as $areatype) {
echo $areatype . ‘<br />’;
}
} else {
echo “You don’t want to live anywhere.<br />”;
}