简体   繁体   English

动态填充两个下拉菜单

[英]Dynamically populate two dropdown menus

I'm not (as I am certain you'll notice below) a programmer or coder. 我不是程序员或编码员(我肯定会在下面注意到)。 I write network documentation manuals. 我写了网络文档手册。

That said, I do have some familiarity with HTML, php, css, and so forth. 也就是说,我确实对HTML,php,css等有所了解。 But what follows has stumped me utterly and completely: 但是,下面的内容完全使我难过:

I am attempting to build a site for a friend of mine who is in a bind, and though everything else is going fine, I do have to redirect the end-user to a webpage built specifically for their community. 我正在尝试为我的一个受束缚的朋友建立一个网站,尽管其他一切都很好,但我确实必须将最终用户重定向到专门为其社区构建的网页。

What I need is a way for the end-user to click on a dropdown menu which lists (through a MySQL query) all of the states where records are available, which, upon their selection of an option within the first menu, a second menu is populated by (again by way of MySQL) the communities within that state. 我需要的是最终用户单击一个下拉菜单的方式,该菜单列出(通过MySQL查询)可以使用记录的所有状态,在第一个菜单中选择一个选项后,便会选择第二个菜单由该州内的社区填充(再次通过MySQL)。

Though I've scoured the internet for things that might be useful, I find that I am simply not well-equipped for the logic of coding. 尽管我在互联网上搜索了可能有用的东西,但我发现我对编码逻辑的理解并不完善。 I did find, however, one site that shows a basic version of what I am trying to do ( see here ), but I can't seem to get it to work with MySQL. 但是,我确实找到了一个站点,该站点显示了我要执行的操作的基本版本( 请参见此处 ),但是我似乎无法使其与MySQL一起使用。 This is where I am so far: 这是我到目前为止的位置:

script -- 脚本-

<script>
 function swapOptions(ArrayName) {
       var ExSelect = document.theForm.sites;
       var theArray = eval(ArrayName);
       setOptionText(ExSelect, theArray);
    }

 function setOptionText(theSelect, theArray) {
       for (loop = 0; loop < theSelect.options.length; loop++) {
           theSelect.options[loop].text = theArray[loop];
       }
    }
 </script>

and HTML -- 和HTML-

 <form name="theForm">
   <select name="chooseCat" onchange="swapOptions(this.options[selectedIndex].text);">
        <option value="">Please select your state ...</option>'
                 <?php 
                    $query = mysql_query("SELECT DISTINCT state FROM sites ORDER BY state") 
                           or die(mysql_error());  
                    while ($result = mysql_fetch_assoc($query)) {   
                           $stateChoice = $result['state'];                                      
                             echo '<option value="';
                             echo "$stateChoice"; 
                             echo '">';
                             echo "$stateChoice";  
                             echo '</option>';
                             echo '<br />';                  
                     }
                  ?>
    </select> 
    <select name="sites" style="min-width: 100px;" onchange="location.href='reports.php?page=' + this.options[this.selectedIndex].value;">
                  <?php 
                     $query = mysql_query("SELECT * FROM sites") 
                            or die(mysql_error());  
                     while ($result = mysql_fetch_assoc($query)) {                                      
                            echo '<option value="';
                            echo '">';
                            echo $result['longname'];  
                            echo '</option>';
                            echo '<br />';                  
                      }
                   ?>
     </select>
 </form>

This returns two dropdowns. 这将返回两个下拉菜单。 The first with the results of my query for the states available, listed alphabetically in the dropdown. 第一个是我对可用状态的查询结果,在下拉列表中按字母顺序列出。 The second has the long-names of all of the sites listed, but for all of the sites in the table, not just the ones within a certain state (the state just chosen). 第二个具有列出的所有站点的长名称,但是对于表中的所有站点,不仅限于某个状态(刚刚选择的状态)内的站点。

In the end, dropdown 1 should be populated with all of the available states. 最后,应该在下拉菜单1中填充所有可用状态。 Once the user selects their state, this should trigger the population of dropdown 2 with all of the communities within their chosen state. 一旦用户选择了他们的状态,这将触发下拉列表2的填充,其中所有社区都处于他们选择的状态内。 Once the choice of community is clicked in dropdown 2, it should redirect the user's browser to, for example, http://www.webpage.com/reports.php?page=communityNameGoesHere ... 在下拉列表2中单击社区选择后,它应该将用户的浏览器重定向到例如http://www.webpage.com/reports.php?page=communityNameGoesHere ...

Thanks so much for any advice you might have here :) 非常感谢您在这里提出的任何建议:)

You have a couple of routes you can go. 您可以走几条路线。 You can use AJAX to grab the values for the sites list upon selection of the state. 选择状态后,可以使用AJAX来获取站点列表的值。 Or, you could simply use javascript to filter the values in the second list based on the selection of the state. 或者,您可以简单地使用javascript根据状态选择来过滤第二个列表中的值。

I might think the second would be a better option for you since it is more straight forward. 我可能会认为第二个对您来说是一个更好的选择,因为它更直接。 So I will focus on that, but if you want an AJAX solution that can be discussed as well. 因此,我将重点讨论这一点,但是如果您希望也可以讨论AJAX解决方案。

I would revise your script to make a single DB call. 我将修改您的脚本以进行单个数据库调用。 Since you are querying the information from one table, there is no reason to call the DB twice. 由于您是从一个表中查询信息,因此没有理由两次调用数据库。

Simply use SELECT * FROM sites ORDER BY states ASC 只需使用SELECT * FROM sites ORDER BY states ASC

You can then loop through the result sets and do something like this 然后,您可以遍历结果集并执行类似的操作

$array = array();
while ($row = mysql_fetch_assoc($query)) {
    $array[$row['state']][] = $row;
}

Then build your first select and populate the options with: 然后构建您的第一个选择,并使用以下选项填充选项:

foreach($array as $state => $not_used) {
    echo sprintf('<option value="%1$s">%1$s</option><br />', $state);
}

Now build the second select and populate the options with: 现在构建第二个选择,并使用以下选项填充选项:

foreach($array as $state => $state_array) {
    foreach($state_array as $site) {
        echo sprintf('<option class="%1$s" value="%2$s">%3$s</option><br />', $state, $site['shortname'], $site['longname']);
    }
}

Note, I have added a HTML class property here. 注意,我在这里添加了HTML class属性。 This makes a convenient handle to show only those items you want to show. 这样可以方便地显示仅要显示的项目。

You give the sites select element an ID of sites , and the states selected element an id of states . 你给站点选择元素的ID sites ,并选择了美国元素的ID states Set following rules in your CSS: 在CSS中设置以下规则:

#sites { display: none; }
#sites option { display: none; }

This will initially hide the sites menu and all the options within it. 最初将隐藏网站菜单及其中的所有选项。

I will now show some simple jQuery for hiding/showing the proper elements, and for making the redirection upon site selection. 现在,我将显示一些简单的jQuery,用于隐藏/显示适当的元素,并在选择站点时进行重定向。 I mention jQuery as this makes what you are trying to do a snap. 我提到jQuery,是因为这使您想要做的很简单。

$(document).ready(function () { // calls this function on document ready
    $('#states').change(function() { // binds onchange function to #states select
        var selected_state = $(this).val(); // gets current selected value of #states select
        $('#sites option').hide(); // hides all options in sites select
        $('.'+selected_state).show(); // show site options with class = selected_state
        $('#sites').show(); // show entire #sites select (only useful for display from initial hidden state)
    });
    $('#sites').change(function() { // binds onchange function to #sites select
        window.location = 'reports.php?page=' + $(this).val(); // perform redirect
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM