I have a form and when someone enters incorrect information, the error is displayed.
For the text fields, I have managed to sort it so the user doesn't have to re-type the information as the value is echoed back to them.
But the problem I have is with an array of checkboxes, so when the user checks some of them, if they made an error in the form, then I can't get the checkboxes to remember which ones were checked.
Here is a snippet of the form...
form.php
<input type="text" name="mobile" value="<?php echo $form->value("mobile"); ?>" />
<label><input type="checkbox" name="optional[]" value="chk1" />box 1</label>
<label><input type="checkbox" name="optional[]" value="chk1" />box 2</label>
<label><input type="checkbox" name="optional[]" value="chk1" />box 3</label>
With the 'mobile' text field, the function value() is called which echos back the value entered...so if the user makes a mistake, then they don't need to re-type their mobile number, as it will be stored for them.
process.php
function value($field) {
if(array_key_exists($field, $this->values)) {
return htmlspecialchars(stripslashes($this->values[$field]));
}
else {
return "";
}
}
I have tried to do the same thing with the checkboxes, but can't figure it out...this is what I have so far...
<input type="checkbox" name="optional[]" value="chk1" <?php if($form->value("mandatory") != ""){ echo "checked == 'checked'"; } ?> />
The problem with this, is if someone checks one checkbox but has an error somewhere else in the form, then the form is sent back to them with all three checkboxes being checked! as they have the same name.
I'm sure this must be a fairly common senario, but can't find the answer on Google anywhere.
I have tried so many variations of this code and haven't managed to get it working...can help me!!!