Log in

View Full Version : Checkboxes - Comma delimited list



jnscollier
08-04-2007, 01:46 AM
i have a browse page where i have checkboxes to allow members to select different age groups. I got the sort part all working, my problem is the boxes that are checked don't stay checked when the page is relaoded to show the new results.

here's what i have, i'm so lost ... if anyone can help i'd really appreciate it!



if (isset($HTTP_POST_VARS['agesort']))
{
$setage = implode(", ", $HTTP_POST_VARS['agesort']);
}
else {
$setage = "2, 5, 3, 6, 4, 7";
}



now if the age group was selected the box should be checked.... i don't know how to do this???




<td width="121"><input type = "checkbox" name = "agesort[]" value = "2" <?php if ($setage == 2) { ?> checked <?php }?>/>
13-17</td>

<td width="114"><input type = "checkbox" name = "agesort[]" value = "5" <?php if ($setage == 5) { ?> checked <?php }?>/>
36-45</td>



Thanks!

Twey
08-04-2007, 06:40 AM
Haha, $HTTP_POST_VARS... haven't seen that one in a while :)
if(isset($_POST['agesort']) && array_reduce($_POST['agesort'], create_function('$a,$b', 'return $a && is_numeric($b);', true)
$setage = $_POST['agesort'];
else
$setage = array(2, 5, 3, 6, 4, 7);

for($i = 0; $i < count($setage); ++$i) {
?>
<td>
<input type="checkbox" name="agesort[]" value="<?php echo $setage[$i]; ?>" checked="checked">
</td>
<?php } ?>

<td width="114"><input type = "checkbox" name = "agesort[]" value = "5" <?php if ($setage == 5) { ?> checked <?php }?>/>Your markup is broken -- you're using XHTML-style self-closing tags, but a binary attribute without a value. The two are mutually exclusive, and I doubt you're serving XHTML anyway (since it would have failed to parse at that). Stick with HTML unless you really know what you're doing.