HTML选择选项集选择默认回显获取值php
问题描述:
我有一个带有这样简单选择的简单HTML表单
I have a simple HTML form with a simple select like this
<select name="myselect" selected="<?php echo $_GET['brand'];?>">
<option value="" <?php if($brand== "") echo "selected"; ?>>all brands</option>
<option value="samsung" <?php if($brand== "samsung") echo "selected"; ?>>Samsung</option>
<option value="motorola" <?php if($brand== "motorola") echo "selected"; ?>>Motorola</option>
</select>
我要尝试的原因是,如果用户选择三星作为默认品牌来搜索带有过滤器搜索结果的下一页,则它是一个搜索过滤结果表,它将使选择选项默认选择三星而不是其他.如果用户选择搜索Motorola,则所选的默认选项将是Motorola.
What I am trying to do is because its a search filtering results form if the user choose Samsung as default brand to search next page with filteres search results will the select option default selected Samsung and no other. If user select to search Motorola then the selected option default will be Motorola.
我该如何解决?
答
selected is attribute of option
not select
, so remove it from select
tag, change to:
<?php $brand = trim( strtolower($_GET['brand']) ); ?>
<select name="myselect">
<option value="" <?php if($brand== "") echo "selected"; ?>>all brands</option>
<option value="samsung" <?php if($brand== "samsung") echo "selected"; ?>>Samsung</option>
<option value="motorola" <?php if($brand== "motorola") echo "selected"; ?>>Motorola</option>
</select>