Page 1 of 1
Dynamically loading select
Posted: Fri May 19, 2006 9:41 pm
by GeXus
When dynamically populating a select box, how would you throw in a selected value? Basically I'm adding data and posting to the same page.. but I would like to have everything populated and selected on return...
Thanks!
Posted: Fri May 19, 2006 9:51 pm
by Nathaniel
The two methods I know of:
1) Add <?PHP if ( $option == 'this_option_value' ): ?>checked="checked"<?PHP endif; ?> in every option tag. For example,
Code: Select all
<option value="this_option_value" <?PHP if ( $option == 'this_option_value' ): ?>checked="checked"<?PHP endif; ?>>This Option Value</option>
2)
Output buffer the select box, and
Code: Select all
str_replace($option, $option . ' checked="checked"', $buffered_output);
where $option equals the value of the option.