mercredi 27 mai 2015

PHP How to set checkboxes to checked in loop?

I wonder how I can make sure a checkbox is checked on a postback if for example someone leaves the addressfield blank it should then make sure the user doesn't have to refill the whole form again. In this case I'm generation 31 checkboxes. It's really frustrating me, please help :)

$output_checkbox = '';
for ($i=1; $i <= 31; $i++) { 
    if (isset($_POST['dagen'])) {
        foreach ($_POST['dagen'] as $dag) {
            if ($dag == $i) {
                $checked . $i = 'checked';
            }
        }
    }
    $output_checkbox .= '<input type="checkbox" name="dagen[]" value="' . $i . '" id="day_' . $i . '"' . $checked . ' /><label for="day_' . $i . '">Dag ' . $i . '</label>';
}




Aucun commentaire:

Enregistrer un commentaire