Setting default value of a drop-down menu in HTML

Soldato
Joined
6 Jun 2005
Posts
2,668
Location
Wirral, UK
Hi guys,

I'm trying to set the default value of a HTML drop-down menu to the value of a PHP variable. The HTML for the menu looks something like this:

Code:
<select size="1" name="cmbSmoker">
<option>-</option>
<option value="Yes">Yes</option>
<option value="No">No</option>
</select>

I know that to set the default value I would add the "SELECTED" tag in like so:

Code:
<option value="Yes" SELECTED>Yes</option>

But how would I set the default value to the value of a variable? For example, if $smoker = 'Yes' then the default value of the drop-down menu would be "Yes".

To achieve the same sort of thing with a text box I have done the following:

Code:
<input type="text" name="txtSmoker" size="5" value=<?php print $smoker ?>>

But I can't see any easy way to do it with a drop-down menu.

Any suggestions much appreciated.
 
Something like:
PHP:
<option value="Yes" <%php if($smoker=='yes') {echo 'SELECTED'} %> Yes</option>

My php is a bit rusty for the exact sytanic but it would look something like that.

I would, also try to use a loop to build your options if possible so you don't have to write loads of tests like above
 
the html you need is actually
Code:
selected="selected"

:)

edit: so it would look like this....

PHP:
<option value="Yes" <?php if($smoker == 'yes') echo 'selected="selected"'; ?>Yes</option>
 
Last edited:
The class:
PHP:
<?php
    class SelectBox {
        private $options = array();
        private $default = '';
        private $attribs = array();

        public function __construct($name, $attribs = false) {
            $this->name = $name;
            $this->attribs = $attribs;
        }

        public function addOption($value, $text = false, $attribs = false, $selected = false) {
            if (!$text) {
                $text = $value;
            }
            $this->options[$value] = array(
                'text'      =>  $text,
                'attribs'   =>  $attribs,
                'selected'  =>  $selected
            );
        }

        public function setDefault($default) {
            if (!array_key_exists($default, $this->options)) {
                throw new Exception('Provided key does not exist');
            }
            foreach ($this->options as $value => $option) {
                if ($option['selected']) {
                    $this->options[$value]['selected'] = false;
                }
            }
            $this->options[$default]['selected'] = true;
        }

        public function toHtml() {
            if (count($this->options) == 0) {
                throw new Exception('List is empty');
            }

            $attribs = '';
            if ($this->attribs) {
                foreach($this->attribs as $name => $value) {
                    $attribs .= ' ' . $name . '="' . $value . '"';
                }
            }

            $output = array();
            $output[] = '<select name="' . $this->name . '"' . $attribs . '>';
            foreach($this->options as $optionval => $option) {
                $attribs = '';
                if ($option['attribs']) {
                    foreach($option['attribs'] as $name => $value) {
                        $attribs .= ' '. $name . '="' . $value . '"';
                    }
                }
                $output[] = "\t" . '<option value="' . $optionval . '"' . $attribs . (($option['selected']) ? ' selected="selected"' : '') . '>' . $option['text'] . '</option>';
            }
            $output[] = '</select>';
            return implode("\n", $output);
        }
    }

Then the declaration:
PHP:
    $s = new SelectBox('foo');
    $s->addOption('10', 'Ten');
    $s->addOption('15', 'Fifteen');
    $s->addOption('20', 'Twenty');
    $s->setDefault('15');
    echo $s->toHTML();

Output:
Code:
<select name="foo">
    <option value="10">Ten</option>
    <option value="15" selected="selected">Fifteen</option>
    <option value="20">Twenty</option>
</select>

A little excessive? Maybe, I was bored. ;)
 
Thanks guys :)

I thought I'd probably end up having to put IF statements in each option. I've done it and it works great.

Moredhel, I've saved your code incase I decide I need more inputs. It's slightly overkill for what I have now but it looks great for a bigger project ;)

Thanks again.
 
Back
Top Bottom