Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - PRE-select drop down option

Tags:

php

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;
like image 292
Phil Avatar asked Jun 13 '10 00:06

Phil


2 Answers

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. For the following, I'll use 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');
like image 60
Joshua Pinter Avatar answered Sep 19 '22 11:09

Joshua Pinter


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 tweaked this function 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;
}
like image 44
Babiker Avatar answered Sep 19 '22 11:09

Babiker