0

私の一日のトラブルは少なくとももう終わってくれることを願っています。ドロップダウンボックスが2つあります。送信した後、アクションページには最初のドロップダウンボックスの値しかありません。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>

これは完全に機能し、最初のドロップダウンボックスから値を選択すると、テーブルの値が2番目のドロップダウンボックスに表示されます。ただし、データを送信すると、$_POSTメソッドを使用してa.phpファイルの最初のドロップダウンボックスの値にのみアクセスできます。つまり、の値しか取得できませんが、$_POST['device']からの値はありません$_POST['screen']。私を助けてください。

4

1 に答える 1

1

finddevice.phpの14行目(上記のとおり)では、2番目のドロップダウンでオプションの値を見逃しているようです。つまり、<optionvalue
>は< optionvalue ="1"> のようになります。id
または値として一意の何かを入力してみてください2番目のドロップダウン
のオプションの場合オプションの値を入力しない限り、2番目のドロップダウンは空白の値をa.phpに送信します

finddevice.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>
于 2012-06-05T20:25:06.007 に答える