Click to See Complete Forum and Search --> : select value


kproc
09-04-2006, 09:02 PM
Hi

I'm trying to set the value of my select box to equal the value of childsex. unfortanilty this does not work.

any idea how to get the value from my php variable to set the select value

<select name="childsex" id="childsex" value="<?php echo $childsex; ?>">
<option></option>
<option>Male</option>
<option selected>Female</option>

</select>

NogDog
09-04-2006, 09:52 PM
You'd have to do something like:

<select name="childsex" id="childsex">
<?php
foreach(array("Male", "Female") as $value)
{
echo "<option";
if($value == $childsex)
{
echo " selected='selected'";
}
echo ">$value</option>\n";
}
?>
</select>

kproc
09-04-2006, 10:04 PM
Thank you for the replay

I'm getting error

Parse error: parse error, unexpected T_ARRAY, expecting '(' in C:\MyServer\xampp\htdocs\familyclick\UpdateChildren.php on line 75

NogDog
09-04-2006, 10:09 PM
Had a closing paren in the wrong place -- I fixed it in my original reply.

kproc
09-04-2006, 10:13 PM
Thank you for the reply.

Is there a way to do the same but to put the values say from 1900 to 2008 in a select option


thank you

NogDog
09-04-2006, 10:39 PM
Instead of the foreach loop, do a for loop:

<select ....... >
<?php
for($yr = 1900; $yr <= 2008; $yr++)
{
echo "<option";
if($yr == $some_variable)
{
echo " selected='selected'";
}
echo ">$yr</option>\n";
}
?>
</select>