无法从第二个动态依赖下拉框中获取值


unable to fetch value from second dynamic dependent drop down box

我希望我一整天的麻烦至少现在能结束。我有 2 个下拉框。提交后,我在操作页面中只有第一个的下拉框值。我的第二个下拉框取决于在第一个下拉框中选择的值。

这是头部部分中的 ajax 代码

    <script>
    function getXMLHTTP() { //function to return the xml http object
            var xmlhttp=false;  
            try{
                xmlhttp=new XMLHttpRequest();
            }
            catch(e)    {       
                try{            
                    xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch(e){
                    try{
                    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
                    }
                    catch(e1){
                        xmlhttp=false;
                    }
                }
            }
            return xmlhttp;
        }

        function getScreen(strURL) {        
            var req = getXMLHTTP();
            if (req) {
                req.onreadystatechange = function() {
                    if (req.readyState == 4) {
                        // only if "OK"
                        if (req.status == 200) {                        
                            document.getElementById('screendiv').innerHTML=req.responseText;                        
                        } else {
                            alert("There was a problem while using XMLHTTP:'n" + req.statusText);
                        }
                    }               
                }           
                req.open("GET", strURL, true);
                req.send(null);
            }
        }
    </script>

这是页面的正文

    <form method="post" action="a.php" name="form1">
    <table width="60%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="150">Device Name</td>
        <td  width="150"><select name="device" onChange="getScreen('finddevice.php?device='+this.value)">
        <option value="">Select device</option>
        <option value="1">Iphone 3</option>
        <option value="2">Iphone 4</option>
        <option value="3">Ipad </option>
        <option value="4">android </option>
            </select></td>
      </tr>
      <tr style="">
        <td>Screen</td>
        <td ><div id="screendiv"><select name="screen">
        <option>Select Screen</option>
            </select></div></td>
      </tr>
      </table>
      <input type="submit" value="submit" />
    </form>

它调用具有以下代码的finddevice.php页面

    <? $device=$_REQUEST['device'];
    $link = mysql_connect('localhost', 'root', ''); //changet the configuration in required
    if (!$link) {
        die('Could not connect: ' . mysql_error());
    }
    mysql_select_db('future');
    $query="select screen from screen where device_id=$device";
    $result=mysql_query($query);
    ?>
    <select name="screen">
    <option>Select Screen</option>
    <? while($row=mysql_fetch_array($result)) { ?>
    <option value><?=$row['screen']?></option>
    <?php $row['device_id'] ?> 
    <? } ?>
    </select>

它工作完美,并在从第一个下拉框中选择值时将表中的值带入第二个下拉框。但是当我提交数据时,我只能使用 $_POST 方法访问 a.php 文件中的第一个下拉框值。这意味着我只能获得$_POST['device']的值,但$_POST['screen']没有价值。请帮帮我。

在 finddevice.php 第 14 行(如上所述)中,您似乎错过了第二个下拉列表中的选项值
我的意思是<期权值>应该是类似<期权值>
尝试在第二个下拉列表中填写 id 或唯一的东西作为选项的值
除非您填写选项的值,否则第二个下拉列表将向 a.php
提交空白值

查找设备.php:

<?php 
    $device=$_REQUEST['device'];
    $link = mysql_connect('localhost', 'root', ''); //changet the configuration in required
    if (!$link) {
        die('Could not connect: ' . mysql_error());
    }
    mysql_select_db('future');
    $query="select id, screen from screen where device_id=$device"; 
        //if screen table contains id field ... 
        //replace id with primary key or any unique identifier for screen table 
        //if there aint any primary key in "screen" table, use a field that is unique 
        //or create a primary key
    $result=mysql_query($query);
?>
<select name="screen">
    <option>Select Screen</option>
    <?php while($row=mysql_fetch_array($result)) { ?>
    <!-- <option value><?=$row['screen']?></option> -->
    <option value="<?php echo $row['id']; ?>"><?php echo $row['screen']; ?></option>
    <?php } ?>
</select>