[英]Show 2nd Dropdown based on First Drop Down PHP JS
PHP/HTML PHP / HTML
<select 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 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);
?>
This gives me correctly two dropdowns with values from database. 这正确地给了我两个带有数据库值的下拉菜单。 http://i.stack.imgur.com/s9rWB.png http://i.stack.imgur.com/4psBC.png (admin please show) How do I get this (using JS or PHP) to make the 'Sub-Category' disabled until a 'Category' is selected, then based on the value chosen in Category, will enable the 'sub-category' dropdown to come up with the values pertaining to the category initially chosen??
http://i.stack.imgur.com/s9rWB.png http://i.stack.imgur.com/4psBC.png (请管理员显示)如何获取此文件(使用JS或PHP)来制作“禁用“子类别”,直到选择了“类别”,然后根据“类别”中选择的值,将启用“子类别”下拉菜单以显示与最初选择的类别有关的值?
The two database tables for category and subcategory are: Category: catID (Pk) catName 类别和子类别的两个数据库表是:类别:catID(Pk)catName
Subcategory: scatID (PK) scatName 子类别:scatID(PK)scatName
(admin please show images) (管理员请显示图片)
Anyone? 任何人?
First of all, you are mixing two independant states and two critical parts of an application. 首先,您要混合应用程序的两个独立状态和两个关键部分。
When a script is generated it is sent to the browser and then there is a downtime until the response comes back. 生成脚本后,会将其发送到浏览器,然后会有一个停机时间,直到响应返回。 But you seem to understand that.
但是你似乎明白这一点。
The other critical part is to rip out the data logic from your script from the view part. 另一个关键部分是从视图部分中提取脚本中的数据逻辑。 No need to have any complex MVC framework to do that, just at least get your data for both parts in the top of your script will make it simpler.
不需要任何复杂的MVC框架即可,至少将脚本两部分的数据都放在脚本的顶部即可使操作变得更加简单。
Here is an approximate way to do it, adapt it to your technology or framework: 这是一种大概的方法,可以使其适应您的技术或框架:
<?php
//Read the data
$data = $data2 = array();
$datares = mysql_query('SELECT * FROM categories');
$data2res = mysql_query('SELECT * FROM subcategories');
while($datarow = mysql_fetch_assoc($datares)){
$data[$datarow['id']] = $datarow;
}
while($data2row = mysql_fetch_assoc($data2res)){
$data2[$data2row['parentid']][$data2row['id']] = $data2row;
}
//If this is a postback
if($_SERVER['REQUEST_METHOD'] == 'POST'){
if(isset($_POST['userListingCateory']) && in_array($_POST['userListingCateory'], array_keys($data))){
$subcategorydata = $data2[$_POST['userListingCategory']];
}
}
/* put all the rest of the code you think you need here */
?>
<select name="userListingCategory">
<?php foreach($data as $dataitem){ ?>
<option value="<?php echo $dataitem['id']; ?>"<?php if($_POST['userListingCategory'] == $dataitem['id']){ echo ' selected="SELECTED"'; } ?> ><?php echo $dataitem['label']; ?></option>
<?php } ?>
</select>
<?php if(isset($subcategorydata)){ ?>
<select name="userListingSubCateory">
<?php foreach($subcategorydata as $dataitem){ ?>
<option value="<?php echo $dataitem['id']; ?>"<?php if($_POST['userListingSubCateory'] == $dataitem['id']){ echo ' selected="SELECTED"'; } ?> ><?php echo $dataitem['label']; ?></option>
<?php } ?>
</select>
<?php } ?>
I hope this will get you started... 我希望这会帮助您入门...
Good luck 祝好运
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.