Welcome Guest, Not a member yet? Register   Sign In
Set select to post-value else use given value
#1

[eluser]Grind[/eluser]
Of a <select> menu, how can display the menu-item that was selected according to the post value and - if the post value isn't set - to a given value? Just like you can do with text fields:
Code:
&lt;input type="text" name="username" value="&lt;?=set_value('username', $userinfo['username'])?&gt;" /&gt;

Is my question clear enough? Smile

Thanks in advance.
#2

[eluser]rogierb[/eluser]
Using set_select() like the almighty user guide says?
#3

[eluser]Grind[/eluser]
I tried, but the third boolean parameter doesn't work for me, because the default selected option is a variable that comes out of a database.
#4

[eluser]Grind[/eluser]
The user guide says this:
Code:
<select name="myselect">
<option value="one" &lt;?php echo set_select('myselect', 'one', TRUE); ?&gt; >One</option>
<option value="two" &lt;?php echo set_select('myselect', 'two'); ?&gt; >Two</option>
<option value="three" &lt;?php echo set_select('myselect', 'three'); ?&gt; >Three</option>
</select>

That would be fine, but in my case all the data is selected out of a database:

Code:
$options = array('one', 'two', 'three');
$default_value = 'one';

echo '<select name="myselect">';
foreach($options as $value)
{
   echo '<option value="'.$value.'" '.set_select('myselect', $value).'>'.$value.'</option>';
}
echo '</select>';

In this case, how can I select the option that's equal to $default_value if no form is submitted? If form submitted, the submitted option must be selected.
Hope this makes it a little bit clearer.
#5

[eluser]mattpointblank[/eluser]
In these situations, I just drop set_select and do something like:

Code:
<option value="123"&lt;?php if($db_value == 123) { echo ' selected="selected"'; } ?&gt;>Test</option>

Oldschool.
#6

[eluser]Grind[/eluser]
Very oldschool Wink
And how do you select the option according to a submitted form? Because in my case the value form the post array must be selected, but if no form is submitted the database value must be selected.
#7

[eluser]bigtony[/eluser]
The third parameter can be anything that resolves to a boolean TRUE or FALSE, so it could be (using your example code):
Code:
$options = array('one', 'two', 'three');
$default_value = 'one';

echo '<select name="myselect">';
foreach($options as $value)
{
   echo '<option value="'.$value.'" '.set_select('myselect', $value, ($value == $default_value)).'>'.$value.'</option>';
}
echo '</select>';
Just do something similar with your db field instead by comparing the db field value to the current loop value.
#8

[eluser]Grind[/eluser]
Of course! Thank you!




Theme © iAndrew 2016 - Forum software by © MyBB