I can't keep the right checkboxes checked after refresh. I have looked at other questions, but nobody seems to deal with this issue where the checkboxes are generated from a for loop (at least I can't find anything on it).
When I check some checkboxes, 3 random ones, I have 3 checked checkboxes when refreshing. However these are always the first 3, instead of the exact ones I checked.
Obviously there is some issue with my code that I should resolve, but it makes me ask another question too. Is it bad practice to generate elements through a loop? I would think it's better as it avoids lines that are more or less duplicates
$numberOfLanguages = count($languageOptions);
for ($i = 0; $i < $numberOfLanguages; $i++) {
$check = '';
if (empty($_POST['language'][$i])) {
$check = '';
}
if (!empty($_POST['language'][$i])) {
$check = 'checked';
}
?>
<li><input type="checkbox" name="language[]" value=" <?php echo $languageOptions[$i] ?>" <?php if($check == 'checked'): ?>checked="checked"<?php endif; ?> /><?php echo $languageOptions[$i] ?></li>
<?php
}
Aucun commentaire:
Enregistrer un commentaire