PHP-预选下拉选项 [英] PHP - PRE-select drop down option
本文介绍了PHP-预选下拉选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在以下URL使用示例: http://www.kavoir.com/2009/02/php-drop-down-list.html
Using the example at the following URL: http://www.kavoir.com/2009/02/php-drop-down-list.html
我如何使该下拉菜单预先选择诸如"Apple"之类的选项之一?
How can I have that drop down menu pre-select one of the options such as 'Apple'?
添加信息
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;
推荐答案
我将向generateSelect
函数添加另一个参数,该参数定义默认值.您可以使用选项的id
或name
来执行此操作.对于以下内容,我将使用name
使其更清晰.
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');
这篇关于PHP-预选下拉选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文