starting a form with different options selected


#1

Hi there!
Okay I have a pull-down menu and I want to have a value selected, based on a variable. Here is part of my code so far, which doesn’t seem to be working:

[code]


10MB
20MB
30MB
50MB
100MB
500MB
1000MB
<?PHP $cboWebSpace = 50; ?>[/code]

For example, this should load the form with the value “50MB” selected.

I know I can use SELECTED in the <option> part to start with a default value, however I often don’t know what the default value will be (Here I have set it as 50, but this is just for convinience. Really it is a variable which can be any of the option values.)

Thank you for taking the time to read my post :) I hope you can help.
Jaggy


#2

Actually, using the selected flag on the option is the only way to do it. You will have to perform a condition in every iteration of the loop that generates the select box.

I also have a method for those days I’m just plain lazy. It implies you know the nature of the data but it works quite fine…

[php]

<?php // Setting a random value, take from your request or whatever $cboWebSpace = 50; ob_start(); ?> 10MB 20MB 30MB 50MB 100MB 500MB 1000MB <?php $content = ob_get_contents(); ob_end_clean();

echo str_replace( " value="{$cboWebSpace}"", " value="{$cboWebSpace}" selected", $content );
?>
[/php]

Basically it captures the output and replaces part of the row that has to be selected for a selected row and send it to the output afterwards.


#3

aha! That works wonderfully. I love it. Thank you very much.