根据选择的第一个下拉列表获取下拉值 [英] Get Values of Dropdown based on First Dropdown selected
问题描述
PHP / MYSQL
<div style="width:810px; margin:inherit; padding-left:170px;">
<select style="width:300px;" id="n" name="userListingCateory">
<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="userListingSubCateory">
<option disabled="disabled">Sub-Category...</option>
<?php while($row = $sth3->fetch(PDO::FETCH_ASSOC))
{echo "<option value=". $row['scatID'] . ">" .$row['scatName']."</option>";}
unset($sth3);
?>
</div>
</select>
上面的HTML获取两个表中的所有类别和子类别 [类别]
和 [SubCategory]
This HTML above gets all categories and sub-categories in two tables [Category]
and [SubCategory]
需要在点击时运行的php / mysql [Category]
下拉列表如下所示:
The php/mysql that needs to run upon clicking the [Category]
dropdown would look like:
SELECT scatID, scatName
FROM Category C, SubCategory SC
WHERE C.catID = SC.catID
AND C.catID = $origCatIDSelected;
如何获得子类别
基于哪个类别
被选中?
How do I get this to get the sub-categories
based on which category
is chosen?
有没有简单的方法在jquery / php中实现?
Is there any easy way to implement in jquery/php?
谢谢
推荐答案
你可以利用 change()执行 AJAX 函数将当前选定的类别传递给包含查询数据库的代码的PHP文件,然后通过内置于 AJAX 函数。
You can utilize the change() event handler that executes an AJAX function to pass the current selected category to a PHP file containing the code to query your database and then return the updated output to the client side via the success callback that is built into the AJAX function within jQuery.
另外,你应该避免对多个对象使用相同的ID,因为ID属性应该是唯一的。
Also, you should avoid using the same ID for multiple objects as the ID attribute is meant to be unique.
这篇关于根据选择的第一个下拉列表获取下拉值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!