Remembering the value of a option box
The following code produces a dynamic dropdown box.
<select name="parent">
<?php
$q = $admindb->getParentCategories();
while($row=mysql_fetch_assoc($q)){
$title=$row['title'];
$catid=$row['id'];
?>
<option value="<?php echo $catid; ?>"><?php echo $title; ?></option>
<?php
}
?>
</select>
However, if the form is returned to the user with validation 开发者_Go百科errors, I need the box to remember the previous selection..
When using a text box, I have used the following code to remember the entry..
<input type="text" name="title" value="<?php echo $form->value("title"); ?>">
How would I add what is in that value tag to the select code?
Thanks
Change your option
line to:
<option value="<?php echo $catid; ?>"<?php if($catid == $form->value("fieldname") echo 'selected="selected"'; ?>><?php echo $title; ?></option>
You have to use the selected
attribute:
<option value="<?php echo $catid; ?>"
<?php if ($_GET['parent'] === $catid): echo 'selected'?>><?php echo $title; ?></option>
If you write XHTML, use selected="selected"
.
You need to add selected="true"
to the correct option like this:
<select name="parent">
<?php
$q = $admindb->getParentCategories();
while($row=mysql_fetch_assoc($q)){
$title=$row['title'];
$catid=$row['id'];
?>
<option value="<?php echo $catid; ?>" <?php if ($catid == $form->value("title")) { echo ' selected="true"';} ?>><?php echo $title; ?></option>
<?php
}
?>
</select>
精彩评论