2017-01-22 84 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