2011-10-19 91 views
-1
 <select style="width:300px;" id="n" name="userListingCategory"> 
       <option disabled="disabled">Category...</option> 
        <?php while($row = $sth2->fetch(PDO::FETCH_ASSOC)) 
        {echo "<option value=". $row['catID'] . ">" .$row['catName']."</option>";} 
       unset($sth2); 
       ?> 

      </select> 
      <select style="width:340px;" id="n" name="userListingSCategory"> 
      <option disabled="disabled">Sub-Category...</option> 
       <?php while($row = $sth3->fetch(PDO::FETCH_ASSOC)) 
        {echo "<option value=". $row['scatID'] . ">" .$row['scatName']."</option>";} 
       unset($sth3); 
      ?> 

這會生成兩個帶值的下拉列表。獲取第一次下拉選擇時的下拉值

在點擊一個userListingCategory,我想下面的SQL運行,以獲得相關的子類別(userListingSCategory):

SELECT scatID, scatName 
FROM Category C, SubCategory SC 
WHERE $valueOfFirstSelected = SC.catID; 

將在本質上,獲得與類別的ID相關聯的子類別編號和名稱最初選定(因此:$valueOfFirstSelected

有人嗎? Ajax或jQuery或Php將是最有益的。

感謝

+0

我認爲你對javascript,php和mysql如何相互交互感到困惑。你可能想看看使用JavaScript和jQuery與Ajax的一些教程,然後使用MySQL與PHP。 – helloandre

回答

0

文件:

<select id="select_one" onchange="FillSelectTwo(this.options[this.selectedIndex].value)"> 
    ... 
</select> 
<select id="select_two"> 
    ... 
</select> 

<script type="text/javascript"> 
function FillSelectTwo(id) { 
    $('#select_two').find('option').remove(); 
    $.ajax({ 
     url: 'ajax_fill_select_two.php?id='+id, 
        dataType: 'text', 
     success: function(data) { 
      $('#select_two').append(data); 
     } 
    }); 
} 
</script> 

ajax_fill_select_two.php:

<!-- of course you need to generate it using $_GET['id'] --> 
<option value="0">text</option> 
<option value="1">text</option> 
<option value="2">text</option> 
<option value="3">text</option> 

最好的解決辦法是發送JSON數據直通Ajax響應,並添加option領域select_two通過javascript

+0

但是這些值ARENT在url中,我該如何使用get here? – Jay

+0

什麼值?工作原理:當用戶在第一個保存箱中更改選定的選項時,您將爲第二個保存箱中的選項發出ajax請求。 – Peter

+0

請參閱更新後的代碼,即「新代碼」部分 – Jay