从数据库中动态下拉菜单但选项值不起作用

发布于 2024-11-27 06:47:13 字数 554 浏览 1 评论 0原文

我正在尝试创建一个动态下拉菜单,从数据库中提取城市名称(并在添加新城市时自动更新),然后在选择时转到该城市页面。下拉菜单有效,但选择城市时没有任何反应。不知道我哪里错了。这就是我得到的

 <?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>";

?>

I'm trying to create a dynamic drop down menu that pulls the city name from a database (and automatically updates as I add new cities) and then when selected goes to that city page. The drop down menu works, but when selecting the city nothing happens. Not sure where I'm going wrong. Here's what I've got

 <?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>";

?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

征﹌骨岁月お 2024-12-04 06:47:13

您可以添加此功能 - 将页面位置更改为所选选项的 value 值 - 通过将以下属性添加到 select

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>";

请注意,您必须设置 < code>value 适合每个城市;我不知道你的数据库架构,所以我无法为你提供建议。

<select> elements to not inherently change the page location when a new option is selected.

You can add this functionality - changing the page location to the value of value for the selected option - by adding the following attribute to select:

onchange="window.location.href=this.options
[this.selectedIndex].value"

i.e.,

<?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>";

Note that you must set value appropriately for each city; I don't know your database schema, so I cannot advise you there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文