PHP $ _POST无法使用2个下拉菜单

时间:2014-12-19 18:51:25

标签: php html forms

我有一个小脚本,显示第2个下拉菜单的值。只有当我发布值时,总是会在PHP脚本中显示菜单的第一项。有什么问题?

HTML:

<head>
    <script type='text/javascript'>//<![CDATA[ 
    function fctCheck(food) {
            var elems = document.getElementsByName("subselector");
            for (var i = 0; i < elems.length; i++) {
                elems.item(i).style.display = "none";
            }
            document.getElementById(food).style.display = "block";
        }//]]>  
    </script>
</head>

<body>
    <form action="test.php" method="post">
        <select id="food" onchange="fctCheck(this.value);">
            <option value="keuze">Choose option</option> 
            <option value="Alanya Gazipasa Airport (GZP)">Alanya Gazipasa Airport (GZP)</option>
            <option value="Antalya Airport (AYT)">Antalya Airport (AYT)</option>
        </select><br>

        <select id="keuze" name="subselector" disabled style="width: 120px;">
            <option value="vanilla">Choose option</option>
        </select>  

        <select id="Alanya Gazipasa Airport (GZP)" name="subselector" style="display:none; width: 120px;">
            <option>Alanya Avsallar</option>
            <option>Alanya Cikcilli</option>
            <option>Alanya Demirtas</option>
            <option>Alanya Kargijak</option>
            <option>Alanya Kestel</option>
        </select>  

        <select id="Antalya Airport (AYT)" name="subselector" style="display:none; width: 120px;">
            <option>Alanya Avsallar</option>
            <option>Alanya Cikcilli</option>
            <option>Alanya Demirtas</option>
            <option>Alanya Incekum</option>
        </select><br>
        <input type="submit">
    </form>
</body>

PHP:

<?php
$value = $_POST['subselector'];
echo "$value";
?>

2 个答案:

答案 0 :(得分:2)

您不能拥有两个具有相同名称的表单元素。一个人会写另一个。您需要为每个人提供一个唯一的名称,然后通过PHP中的唯一名称访问它们。

答案 1 :(得分:1)

无需更改名称提交bcoz你想要名字选择器你必须禁用无关的 选择框,因为如果我隐藏该元素但执行时未显示使用

试试这个工作正常

     <script type='text/javascript'>//<![CDATA[ 
           function fctCheck(food) {
           var elems = document.getElementsByName("subselector");
           for (var i = 0; i < elems.length; i++) {
               elems.item(i).style.display = "none";
               elems.item(i).disabled = true;
           }
          document.getElementById(food).style.display = "block";
          document.getElementById(food).disabled = false;
          }
     </script>