HTML选择选项集选择默认回显获取值php [英] HTML select option set selected default echo get value php

查看:39
本文介绍了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>

这篇关于HTML选择选项集选择默认回显获取值php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆