简体   繁体   English

更新MultiSelect下拉列表

[英]Updating a MultiSelect Dropdown

My webpage works like this: 我的网页是这样的:

I have a multiselect dropdown that is populated by two database tables. 我有一个由两个数据库表填充的多选下拉列表。 The one table has all of the values and the other has the selected values. 一个表具有所有值,另一个表具有选定的值。 (This part cannot be changed) (此部分无法更改)

Once the dropdown is populated and the correct values are selected, the user then has the option to select more values in the list and that would transfer in an array to another page. 一旦填充了下拉列表并选择了正确的值,用户就可以选择在列表中选择更多的值,这些值将以数组的形式转移到另一个页面。

function getGrade($id, $grades_array)
{
        $counter = 0;
        $sql = "Select grade FROM grades";
        $result = mysql_query($sql) or die (mysql_error());
        echo '<select name="grade" multiple="multiple" id="grades_selected">';
        while ($row = mysql_fetch_array($result)) {

                if ($row['grade'] != $grades_array[$counter]) {
                        echo "<option>" . $row['grade'] . "</option>";
                } else {
                        echo "<option selected=" . $row['grade']  . ">" . $row['grade'] . "</option>";
                        $counter = $counter + 1;
                }
        }
    mysql_free_result($result);
        echo '</select>';
}

The initial variable $grades_array is the selected values. 初始变量$ grades_array是选定的值。

In words the solution I need is to be able to pull the values from the database on page load but if the user selects more values then it will be able to add those to the array and save that variable to use on the other page. 换句话说,我需要的解决方案是能够在页面加载时从数据库中提取值,但是如果用户选择更多值,那么它将能够将这些值添加到数组中并保存该变量以在另一页面上使用。 The hardest part I think is then when the user comes back to the page for the 2nd time it does not pull from the database again and uses the same created variable to populate the dropdown. 我认为最难的部分是,当用户第二次返回该页面时,它不会再次从数据库中拉出,而是使用相同的已创建变量来填充下拉列表。

Someone Please Help! 有人请帮忙!

You can try this by using this AJAX, easier to do rather than doing it with PHP 您可以通过使用此AJAX进行尝试,这比使用PHP轻松实现

suggestion text field like -- google suggestion - using AJAX/Jquery 建议文本字段,例如-谷歌建议-使用AJAX / Jquery

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

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