Page 1 of 1
Keeping checkbox values on error...
Posted: Wed Jul 28, 2004 12:00 pm
by incyte
I can't seem to figure out a way to keep the checkbox value when an error occurs on the form.
The value is there but the checkbox doesnt remain selected when the errors come up , if there are any.
i have my form set up in a block:
$form = " <html code here>....";
so when my error checking goes thru it echo's $form and the error messages on the missing or invalid fields. How do i keep the selected checkbox selected...if that made any sense.
Posted: Wed Jul 28, 2004 12:12 pm
by feyd
you need to add something that will add "selected" to the fields output so it'll look like this:
Code: Select all
<input type="checkbox" name="foo" value="bar" selected="selected" />I am a selected checkbox<br />
<input type="checkbox" name="foo" value="bif" />I am an unselected checkbox<br />
Posted: Wed Jul 28, 2004 12:16 pm
by incyte
yea i figured that just dunno how to put it in there using the "block"
was gonna put this after the value of the box
Code: Select all
<?php
<? if ($gender == male) { echo " selected"; } ?>
if i put that in tho i just get a parse error....sayin something bout T_STRING T_VARIABLE something like that..
?>
Posted: Wed Jul 28, 2004 12:31 pm
by feyd
I use something similar to this:
Code: Select all
<?php
function constructInput($type,$name,$value,$error = false,$selected = false)
{
if(in_array($type,array('checkbox','radio')))
return '<input type="' . $type . '" name="' . $name . '" value="' . $value . '" ' . ((bool)$selected ? 'selected="selected" ' : '') . '/>' . (is_string($error) ? $error : '');
else
return '<input type="' . $type . '" name="' . $name . '" value="' . $value . '" />' . (is_string($error) ? $error : '');
}
?>
still probs
Posted: Wed Jul 28, 2004 1:20 pm
by incyte
tried this
Code: Select all
if ($_POSTї$gender] = "male") {
$app_block2 = "
<input name="gender" type="radio" value="male" selected>
Male
<input name="gender" type="radio" value="female">
Female";
}
else if ($_POSTї$gender] = "female") {
$app_block2 = "
<input name="gender" type="radio" value="male" >
Male
<input name="gender" type="radio" value="female" selected>
Female";
}
else
$app_block2 = "
<input name="gender" type="radio" value="male">
Male
<input name="gender" type="radio" value="female">
Female";
still not working right figured that would do it but nope...

ideas?
Posted: Wed Jul 28, 2004 2:41 pm
by Draco_03
you don't pass a vlaue to your "selected"
must be selected = "selected"
still not working
Posted: Mon Aug 02, 2004 11:05 am
by incyte
still not working...
now when it loads it automatically checks the male value