tags:

views:

34

answers:

2

Using the example at the following URL: http://www.kavoir.com/2009/02/php-drop-down-list.html

How can I have that drop down menu pre-select one of the options such as 'Apple'?

EDIT: Addition info

function showForm()

{
global $sProduct, $name, $product, $header_file, $footer_file, $form_width, $form_background, $form_border_color, $form_border_width, $form_border_style, $cell_padding, $left_col_width, $font_size;   
include $header_file; 
echo <<<EOD

<form method="post" class="cForm">
<table style="width:{$form_width}; padding:20px 7px 0px 7px; font-size:{$font_size}; background-color:{$form_background};" class="contactForm">
<tr>
<td style="width:{$left_col_width}; text-align:left; vertical-align:center; padding:{$cell_padding}; font-weight:bold; {$name[3]}">{$name[0]}</td>
<td style="text-align:left; vertical-align:top; padding:{$cell_padding};"><input type="text" name="{$name[1]}" value="{$name[2]}" size="25"/></td>
</tr>
<tr>
<td style="width:{$left_col_width}; text-align:left; vertical-align:center; padding:{$cell_padding}; font-weight:bold;">{$product[0]}</td>
<!--<td style="text-align:left; vertical-align:top; padding:{$cell_padding};"><select name="{$product[1]}">-->
<td style="text-align:left; vertical-align:top; padding:{$cell_padding};">
<select name="{$product[1]}">
    <option value="1"></option>
    <option value="2">item 2</option>
    <option value="3">item 3</option>
    <option value="4">item 4</option>
    <option value="5">item 5</option>
    <option value="6">item 6</option>
</select>
</td>
</tr>
<td style="width:{$left_col_width}; text-align:left; vertical-align:center; padding:{$cell_padding}; font-weight:bold; {$code[3]}">{$code[0]}</td>
<td style="text-align:left; vertical-align:top; padding:{$cell_padding};"><input type="submit" name="submit" value="Submit" style="border:1px solid #999;background:#E4E4E4;" /></td>
</tr>
<tr>
<td colspan="2" style="font-size:10px; text-align:left; vertical-align:middle; padding:{$cell_padding};">
0:(var_product[0] = ({$product[0]})) <br/>
1:(var_product[1] = ({$product[1]})) <br/>
2:(var_product[2] = ({$product[2]}))<br/>
3:(var_product[3] = ({$product[3]}))<br/>
4:(var_sProduct = ({$sProduct}))<br/>
</td>
</tr>
</table>
</form>

EOD;
+1  A: 

When you echo your option, echo it as follows:

<option value="apple" selected="selected">Apple</option>

Or you can use js as follows:

var dropDownList = document.getElementById('dropDownListId');
dropDownList.options[optionIndex].selected = true;

I tweeked this fucntion a bit but you can use it:

function generateSelect($name, $options, $optionToSelect) {
    $html = '<select name="'.$name.'">';
    foreach ($options as $option => $value) {
        if($value == $optionToSelect)
            $html .= '<option value="'.$value.'" selected="selected">'.$value.'</option>';
        else
            $html .= '<option value="'.$value.'">'.$value.'</option>';
    }
    $html .= '</select>';
    return $html;
}
Babiker
I'm not sure how to format it correctly. I like the simplicity of your answer and hope I can get it to work.I have my drop down menu in a table. I have edited my original post to show the code of my echo. Would you mind taking a look at it? I have greatly simplified it.
Phil
This line: function generateSelect($name, $options, $optionToSelect) {helped me to better understand what was going on in this functionThank you very much!
Phil
FYI, by adding $name = '', all it does is just set a default value so that if a specific value isn't entered, it defaults to whatever is there. The $name and $options are required but I would make $optionToSelect optional by doing $optionToSelect = ''
Josh Pinter
+2  A: 

I would throw another parameter into the generateSelect function that defines what the default is. You can do this with either the id of the option or by the name but for the following, I'll use the name to make it clearer.

function generateSelect($name = '', $options = array(), $default = '') {
    $html = '<select name="'.$name.'">';
    foreach ($options as $option => $value) {
        if ($option == $default) {
            $html .= '<option value='.$value.' selected="selected">'.$option.'</option>';
        } else {
            $html .= '<option value='.$value.'>'.$option.'</option>';
        }
    }

    $html .= '</select>';
    return $html;
}

/* And then call it like */
$html = generateSelect('company', $companies, 'Apple');
Josh Pinter
gotit. My problem was that I wasn't using "html" in my table. instead I was actually typing:<select name="{$product[1]}"> <option value="1"></option> <option value="2">item 2</option> <option value="3">item 3</option> <option value="4">item 4</option> <option value="5">item 5</option> <option value="6">item 6</option></select>by placing $html in its place and using your code, it works perfectly now. Thank you very much
Phil
Excellent. This is where PHP (and any dynamic language for that matter) really shines, by automating repetitive tasks. Keep rockin'
Josh Pinter