如何在php中获取下拉值

时间:2015-04-21 06:51:35

标签: javascript php html

大家好我正在开发PHP应用程序,我根据另一个使用 javascript 的投递箱选项填充了一个投递箱 - 一切正常但第二个投递箱值是数字,我的问题是如何将第二个保管箱的值设置为文本而不是数字,这是我得到的。

我有以下代码用于下拉框:

    <!-- Our food group drop down-->
   <select id="foodgroups" onchange="selectFoods(this.options[this.selectedIndex].id)">
        <option id="1" value="">Select a Food Group</option>
        <option id="2"value="Fruit">Fruit</option>
        <option id="3" value="Vegetables">Vegetables</option>
        <option id="4" value="Meat">Meat</option>
        <option id="5" value="Dairy">Dairy</option>
        <option id="6" value="Grains">Grains</option>
    </select>

    <!-- Our foods for a group selection (empty on default) -->


 <select id="foods" name="selectedFoodItem"></select>

这是填充第二个下拉列表的函数

 function selectFoods(foodGroup) {
        // Get a reference to the foods select.
        var foodsSelect = document.getElementById("foods");

        // Create a two dimension array containing foods for each group.
        var foods = [["Banana","Grapes","Oranges","Watermelon"],
                    ["Brocolli", "Califlower","Spinach","Tomatoes"],
                    ["Beef", "Chicken", "Turkey", "Tuna"],
                    ["Cheese", "Ice Cream", "Milk", "Yogurt"],
                    ["Bread", "Pasta", "Rice", "Quinoa"]];

        if ((foodGroup >= 0) && (foodGroup <= foods.length)) {
            foodsSelect.options.length = 0;

            // Index was in range, so access our array and create options.
            for (var i = 0; i < foods[foodGroup - 1].length; i++) {
               foodsSelect.options[foodsSelect.options.length] = new Option(foods[foodGroup - 1][i], i);
            }

        }
    }

我试图访问php $val = $_POST['selectedFoodItem'];中的值 我得到了索引

谢谢!

3 个答案:

答案 0 :(得分:1)

由于ID从2开始(有效的)和你的食物选择索引从2开始,只需在函数运行后得到ID - 2,然后将其用作数组选择中的索引,将所有这些值循环到内部以创建标记:

function selectFoods(foodGroup) {
    // Get a reference to the foods select.
    var foodsSelect = document.getElementById("foods");

    // Create a two dimension array containing foods for each group.
    var foods = [["Banana","Grapes","Oranges","Watermelon"],
                ["Brocolli", "Califlower","Spinach","Tomatoes"],
                ["Beef", "Chicken", "Turkey", "Tuna"],
                ["Cheese", "Ice Cream", "Milk", "Yogurt"],
                ["Bread", "Pasta", "Rice", "Quinoa"]];

    var options = '';
    var selected = foodGroup.id - 2; // get the ID minus 2
    var l = foods[selected].length;
    for(var i = 0; i < l; i++) { // loop foods and get the selected thru the index selected
        options += '<option value="' + foods[selected][i] + '">' + foods[selected][i] + '</option>';
    }

    foodsSelect.innerHTML = options;
}

Sample Output

答案 1 :(得分:1)

由于这行代码,您的PHP代码会获取索引而不是您期望的字符串值:

new Option(foods[foodGroup - 1][i], i);

第一个参数是您在select中看到的值,而第二个参数是提交给服务器端的值。在您的示例中,其中一个选项将呈现如下:

<option value="0">Brocolli</option>

如果您希望服务器端接收"Broccoli",请更改选项代码,如下所示:

new Option(foods[foodGroup - 1][i], foods[foodGroup - 1][i]);

导致:

<option value="Brocolli">Brocolli</option>

示例http://jsfiddle.net/4298w4ob/

答案 2 :(得分:1)

更改此脚本代码。

function selectFoods(foodGroup) {
        // Get a reference to the foods select.
        var foodsSelect = document.getElementById("foods");

        // Create a two dimension array containing foods for each group.
        var foods = [["Banana","Grapes","Oranges","Watermelon"],
                    ["Brocolli", "Califlower","Spinach","Tomatoes"],
                    ["Beef", "Chicken", "Turkey", "Tuna"],
                    ["Cheese", "Ice Cream", "Milk", "Yogurt"],
                    ["Bread", "Pasta", "Rice", "Quinoa"]];

        if ((foodGroup > 1) && ((foodGroup-1) <= foods.length)) {
            foodsSelect.options.length = 0;

            // Index was in range, so access our array and create options.
            for (var i = 0; i < foods[foodGroup - 2].length; i++) {

               foodsSelect.options[foodsSelect.options.length] = new Option(foods[foodGroup - 2][i], foods[foodGroup - 2][i]);
            }

        }
}

您将获得所需的答案。 :)