获取首次下拉选择的下拉值

时间:2011-10-19 19:15:38

标签: php mysql variables

        <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相关联的SubCategory ID和名称(因此:$valueOfFirstSelected

任何? Ajax或jQuery或Php将是最有益的。

由于

1 个答案:

答案 0 :(得分: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>

最佳解决方案是通过ajax响应发送JSON数据,并通过javascript将option字段添加到select_two