Reputation: 1159
I have a form with several check boxes populated from a db
CODE:
// populating Checkboxes from db
echo '<div>
<label for="'.$n['eName'].'">'.$n['eName'].'</label>
<input type="checkbox" name="skills[]" id="'.$n['eName'].'" value="'.$n['id'].'" '.(isset($_POST[$n['eName']]) ? 'checked="checked"' : '') .' />
</div>';
The problem is when the user select some of these check-boxes and submit the form and get error, the form can not remember his choices and he has to re-select them again.
what can I do to go over this issue?
Thanks in advance
Upvotes: 0
Views: 39
Reputation: 1949
This is not correct:
isset($_POST[$n['eName']]
You should look in the $_POST['skills'] array.
According to your implementation of $n['eName'] the following code might work:
echo '<div>
<label for="'.$n['eName'].'">'.$n['eName'].'</label>
<input type="checkbox" name="skills['.$n['eName'].']" id="'.$n['eName'].'" value="'.$n['id'].'" '.(isset($_POST['skills'][$n['eName']]) ? 'checked="checked"' : '') .' />
</div>';
ps: Be warned that any quotation in $n['eName'] would most likely break your code - one should take measures for these cases.
Edit:
<form method=post>
<?php
$n['id']='someid';
$n['eName'] = 'test';
echo '<div>
<label for="'.$n['eName'].'">'.$n['eName'].'</label>
<input type="checkbox" name="skills['.$n['eName'].']" id="'.$n['eName'].'" value="'.$n['id'].'" '.(isset($_POST['skills'][$n['eName']]) ? 'checked="checked"' : '') .' />
</div>';
?>
<input type=submit >
</form>
Upvotes: 1