选择第一个下拉列表时获取下拉列表的值

发布于 2024-12-11 03:49:42 字数 1216 浏览 0 评论 0原文

        <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 关联的子类别 ID 和名称最初选择的类别的名称(因此:$valueOfFirstSelected

有人吗? Ajax 或 jQuery 或 Php 将是最有益的。

谢谢

        <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);
             ?>

This makes two dropdown lists with values.

Upon clicking a userListingCategory, I want the following SQL to run to get the associated subcategories (userListingSCategory):

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

Which will in essence, get the SubCategory ID and Name associated with the ID of the Category originally selected (hence: $valueOfFirstSelected)

Anyone? Ajax or jQuery or Php would be most beneficial.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

后来的我们 2024-12-18 03:49:42

你的文件:

<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数据并添加option字段通过 JavaScript 选择两个

your file:

<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>

the best solution is send JSON data thru ajax response and add option fields to select_two via javascript

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文