2012-06-04 17 views
0

我希望我的整個一天的麻煩現在至少會結束。我有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']沒有價值。請幫助我。

回答

1

在finddevice.php,第14行(如上),你似乎已經錯過了第二個下拉
我的意思是<期權價值>應該像<期權價值=「1」 >選項的值在ID或一些獨特的價值在第二滴選項
嘗試充絨
除非你在選項填入值,第二個下拉將提交空白值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> 
相關問題