我试图创建一个动态下拉菜单,从数据库中拉城市名称(并在添加新城市时自动更新),然后选择到该城市页面。下拉菜单可以工作,但是当选择城市时什么都没有发生。不知道我哪里做错了。这是我得到的
<?php mysql_select_db ("db_name");
echo "<select name=database><option value='.'>Select Your City</option>";
$result = mysql_query ("select DISTINCTROW city_head from database order by city_head");
while ($city_head=mysql_fetch_assoc($database)) {
echo "<option value="#CityIDPage">".$city_head[city_head]."</option>'n"; }
echo "</select><p>";
?>
<select>
元素不会在选择新选项时固有地改变页面位置。
您可以通过向select
添加以下属性来添加此功能-将所选选项的页面位置更改为value
的值:
onchange="window.location.href=this.options
[this.selectedIndex].value"
。
<?php
mysql_select_db ("db_name");
echo "<select name=database onchange='"window.location.href=this.options
[this.selectedIndex].value'"><option value='.'>Select Your City</option>";
$result = mysql_query ("select DISTINCTROW city_head from database order by city_head");
while ($city_head=mysql_fetch_assoc($database)) {
echo "<option value="#CityIDPage">".$city_head[city_head]."</option>'n";
}
echo "</select><p>";
注意,您必须为每个城市适当地设置value
;我不知道你的数据库模式,所以我不能给你建议。