Re: default option
| From: | Chris Stinemetz | Date: | Tue, 19 Jul 2011 03:59:29 +0000 |
| Subject: | Re: default option | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-314112@lists.php.net to get a copy of this message | ||
Thank you Stuart. That did the trick.
On Mon, Jul 18, 2011 at 10:38 PM, Stuart Dallas <stuart@3ft9.com> wrote:
> On Tue, Jul 19, 2011 at 4:28 AM, Chris Stinemetz <chrisstinemetz@gmail.com>
> wrote:
>>
>> Hello,
>>
>> I am building some select menu's dynamically from a mysql database and
>> am courous how to give the menu a default option "Choose".
>>
>> Below is what I have so far for one of my menu's.
>>
>> Thank you in advace.
>>
>> <select name="market" id="market"
>> onchange="javascript:get(this.parentNode);">
>> <?php
>> foreach($market_prefix as $key => $value)
>> {
>> $selected = '';
>> if($value == $market)
>> {
>> $selected = 'selected';
>> }
>> echo("<option value=$value $selected
>> >$value : $market_name[$key]");
>> }
>> ?>
>> </select>
>
> Just add it as the first option. If none of the options have the selected
> attribute, the first option will be the selected one.
> I'd also recommend you escape the variables you're outputting, on the off
> chance they contain HTML-like code.
> <select name="market" id="market" onchange="if (this.value !=
> '') {
> javascript:get(this.parentNode); }">
> <option value="">Choose...</option>
> <?php
> foreach($market_prefix as $key => $value)
> {
> $selected = '';
> if ($value == $market)
> {
> $selected = 'selected';
> }
> echo '<option value="', htmlspecialchars($value), '" ',
> $selected, '>',
> htmlspecialchars($value.' : '.$market_name[$key]), '</option>';
> }
> ?>
> </select>
> -Stuart
>
> --
> Stuart Dallas
> 3ft9 Ltd
> http://3ft9.com/