2014-12-05 102 views
0
1) when types selected from dropdown his category and subcategory should be shown 
    in dropdown,problem : when we select types in category it is showing all the category 
    and all the subcategory of other types also 
    please solve this problem as soon as possible. 

    inventory_list.php 
    <select name="typ_id" id="typ_id"> 
    <option>Select a Type</option> 
     <?php 
     $get_types = "select * from types"; 
     $run_types = mysql_query($get_types); 
    while ($row_types=mysql_fetch_array($run_types)){ 
      $typ_id= $row_types['typ_id']; 
     $typ_name = $row_types['typ_name']; 
     echo "<option value='$typ_id'>$typ_name</option>";  
     } 
    ?> 

    <select name="cat_id" id="cat_id"> 
    <option>Select a Category</option> 
    <?php 
    $get_category = "SELECT * FROM category "; 
    $run_category = mysql_query($get_category); 
    while ($row_category=mysql_fetch_array($run_category)){ 
     $cat_id= $row_category['cat_id']; 
     $cat_name = $row_category['cat_name']; 
     echo "<option value='$cat_id'>$cat_name</option>";  
     } 
    ?> 
    </select>   
+0

请查看教程MySQL查询与WHERE条件。 https://www.google.co.in/?gfe_rd=cr&ei=AzaBVNihLtLAuASLo4GgDw&gws_rd=ssl#q=mysql+select+with+condition – Chandresh 2014-12-05 04:36:27

+0

你也可以参考这个链接相同:http://stackoverflow.com/questions/20795351/dynamic-dropdownlist-value-from-database/20795573#20795573 – Chandresh 2014-12-05 04:38:47

回答

0

尝试下面的代码

<script> 
function get_category(type_id){ 
     $.ajax({ 
      type: "POST", 
      url: "get_category.php", 
      data: { type_id : type_id } 
     }).done(function(data){ 
      $("#category").html(data); 
     }); 
    } 
</script> 

<select name="typ_id" id="typ_id" onchange="get_category(this)"> 
    <option>Select a Type</option> 
     <?php 
     $get_types = "select * from types"; 
     $run_types = mysql_query($get_types); 
    while ($row_types=mysql_fetch_array($run_types)){ 
      $typ_id= $row_types['typ_id']; 
     $typ_name = $row_types['typ_name']; 
     echo "<option value='$typ_id'>$typ_name</option>";  
     } 
    ?> 

<div id='category'></div> 

get_category.php

<?php 
     $get_category = "SELECT * FROM category where type_id = $_POST['type_id']"; 
     $run_category = mysql_query($get_category); 
     echo '<select name="cat_id" id="cat_id"> 
     <option>Select a Category</option>'; 

     while ($row_category=mysql_fetch_array($run_category)){ 
      $cat_id= $row_category['cat_id']; 
      $cat_name = $row_category['cat_name']; 
      echo "<option value='$cat_id'>$cat_name</option>";  
      } 
    echo '</select>'; 

?> 
+0

从下拉菜单中选择类型后,下拉菜单不会出现,什么是类别的html。 – 2014-12-05 08:42:40

+0

使用平变化= “get_category(THIS.VALUE)”,而不是平变化= “get_category(这)” 和不要忘记包括jquery的文件 – manuyd 2014-12-05 09:20:44

+0

inventory_list.php 类型 ​​