2017-01-22 45 views
0

這是我的PHP和HTML職業生涯的早期階段。我想填充一個SQL查詢 - 沒問題。但是,接下來,從該查詢中取出突出顯示的值,並使用它來驅動另一個查詢,以填充另一個下拉列表。如何使用前一個下拉菜單的結果加載下拉查詢

我不明白的是如何在HTML和PHP之間傳遞值,我應該使用全局變量來存儲第一個下拉列表中的選擇嗎?我該怎麼做呢?

使用按鈕指示第一個下拉列表中當前突出顯示的值將用於驅動填充第二個下拉列表的查詢時,是否有其他替代方法?

我的代碼如下所示:

Asset type:<br/> 
    <select size=3 class="element select medium" id="WantAssetCategory" name="WantAsset Class"> 
     <? 
     $sql = "SELECT * from Categories"; 

     $result = $dbcon->query($sql); 
     foreach ($result as $row){ 
      $catname = $row['description']; 
      $catx = $row['id']; 
      echo '<option value = ' . "$catx> $catname</option>" ; 
     } 
     $result = null; 
     ?>      
    </select> 
+0

您可以使用Ajax運行第一個選擇下拉列表的更改並獲取所選值,然後使用該值運行查詢。在ajax調用成功的時候,創建新的選擇下拉菜單,預填充查詢的結果,並用它替換第二個下拉html。我希望它可以幫助我嘗試爲它寫一個演示代碼,如果這是你需要的。 – sT0n3

回答

0
<select size=3 class="element select medium" id="WantAssetCategory" name="WantAsset Class"> 

使用jQuery,聽取他們對選擇的變化,並與選定的值進行動作腳本,在響應更新的選項爲新的下拉與提到的ID。

document.getElementById("WantAssetCategory").onchange = function() { 
    $.ajax({ 
       data: this.value, // This is the selected value from your drop down. 
       type: "POST", // GET or POST 
       url: "query.php", // the file(where the query is) to call 
       success: function(response) { 
        $('#output').html(response);// repose response should contain the new select options and #output should be the id of the new select drop down. 
       } 
      }); 
} 

我希望有幫助。

+0

這看起來非常有前途,謝謝! – SirHowy

相關問題