简体   繁体   English

显示基于First Drop Down PHP JS的第二个下拉列表

[英]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.

相关问题 通过在codeigniter php中使用ajax,根据第一个下拉结果填充第二个下拉列表 - populate 2nd drop down based on 1st drop down result by using ajax in codeigniter php 根据选择的第一个下拉列表获取第二个下拉列表值 - Get 2nd Dropdown Value Based on First Dropdown Chosen 通过选择php中的第一个下拉列表值来填充第二个下拉列表 - populate 2nd dropdown by select first dropdown value in php 根据PHP中选择的第一个下拉列表填充第二个下拉列表 - Populate 2nd dropdown based on 1st dropdown selected in PHP 无法使用getJSON和php填充第二个下拉列表 - Failed to fill 2nd drop down list with getJSON and php 在第一个下拉菜单被选中后显示第二个下拉菜单的内容。 - Show content of 2nd Drop down after 1st Drop down getting selected. 使用 API JSON 根据第一个下拉列表填充第二个下拉列表 - Populate 2nd drop-down list based on 1st drop-down using API JSON 我如何根据php ajax请求中的第一个下拉列表在下拉列表中获取记录 - How do i get record in a dropdown list based on a first drop down list in php ajax request 如何根据从第一个下拉菜单自动更改的第二个下拉菜单更改第二个文本框的值? - How to change the 2nd textbox value based on the 2nd dropdown that is automatically changed from 1st dropdown? 基于第一个下拉列表的动态第二个下拉列表 - Dynamic 2nd Dropdown-list based on the 1st Dropdown
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM