Solve me Please!
Posted: Fri Aug 15, 2008 6:36 am
Hi, I have a registration form that returns the errors to the user when they make a mistake, ie, they don't then have to re-type the data.
This works for the text fields, but i can't get it to work for the checkboxes. Here is some of the code as an example...
form.php
With the 'mobile' text field, the php points to a function value() listed below, which echoes back the value entered to the name of the field, ie, mobile.
process.php
I have tried the same thing with the checkboxes, but to no success. The problem is that the checkboxes all have the name of 'optional', so if the user checks one of them, and there is an error elsewhere in the form, it is returned to them but the script checks any box with the name of optional, which is all of them!! See below...
I can't find the solution to this anywhere on google as everything I type about returning form values or storing form data comes up with hits about databases, which is not what I'm looking for.
I have also tried many variations of the code above with no success...can anyone please help?
This works for the text fields, but i can't get it to work for the checkboxes. Here is some of the code as an example...
form.php
Code: Select all
<input type="text" name="mobile" value="<?php echo $form->value("mobile"); ?>" />
<lable><input type="checkbox" name="optional[]" value="chk1" />Box 1</label>
<lable><input type="checkbox" name="optional[]" value="chk2" />Box 2</label>
<lable><input type="checkbox" name="optional[]" value="chk3" />Box 3</label>
process.php
Code: Select all
function value($field) {
if(array_key_exists($field, $this->values)) {
return htmlspecialchars(stripslashes($this->values[$field]));
}
else {
return "";
}
}
Code: Select all
<input type="checkbox" name="optional[]" value="chk1" <?php if($form->value("optional") !=""){ echo "checked == 'checked'"; } ?> />
I have also tried many variations of the code above with no success...can anyone please help?