2014-10-08 91 views
2

此代碼是兩個類別和subcategory.In類我顯示在下拉列表中的所有值,但子類別我想說明這是屬於一個類別的準確子類別。請幫我如何顯示屬於某個類別的子類別?

<div class="form-group"> 
    <label for="exampleInputEmail1">Category Name</label> 
    <select name="type" id="type"> 
     <option>Select Category</option> 
     <?php 
      $res= mysql_query("select * from ".CATEGORY." order by id asc"); 
      while($data=mysql_fetch_array($res)) 
      { 
     ?> 
     <option value="<?php echo $data['id'];?>"><?php echo $data['cat_name'];?></option> 
     <?php } ?> 
    </select> 
</div> 
<div class="form-group"> 
    <label for="exampleInputEmail1">Subcategory Name</label> 
    <select name="type1" id="type1"> 
     <option>Select Category</option> 
     <?php $res= mysql_query("select * from ".SUBCATEGORY." order by id asc"); 
     while($data=mysql_fetch_array($res)) 
     { 
     ?> 
     <option value="<?php echo $data['id'];?>"><?php echo $data['sub_name'];?></option> 
     <?php } ?> 
    </select> 
</div>        
+0

使用AJAX來獲取子類 – Khushboo 2014-10-08 09:28:52

回答

5

在它使用jQuery時,一類是選擇將需要價值和從阿賈克斯價值就會發布到ajax.php文件

<div class="form-group"> 
    <label for="exampleInputEmail1">Category Name</label> 
    <select name="type" id="type"> 
     <option>Select Category</option> 
     <?php 
      $res= mysql_query("select * from ".CATEGORY." order by id asc"); 
      while($data=mysql_fetch_array($res)) 
      { 
     ?> 
     <option value="<?php echo $data['id'];?>"><?php echo $data['cat_name'];?></option> 
     <?php } ?> 
    </select> 
</div> 
<div class="form-group"> 
    <label for="exampleInputEmail1">Subcategory Name</label> 
    <select name="type1" id="type1"> 


    </select> 
</div> 


<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script> 
<script type="text/javascript"> 
$(document).ready(function(){ 

    $('#type').on("change",function() { 
     var categoryId = $(this).find('option:selected').val(); 
     $.ajax({ 
      url: "ajax.php", 
      type: "POST", 
      data: "categoryId="+categoryId, 
      success: function (response) { 
       console.log(response); 
       $("#type1").html(response); 
      }, 
     }); 
    }); 

}); 

</script> 

在同一目錄

做一個PHP文件命名爲ajax.php,並把這個代碼

<?php 
$categoryId = $_POST['categoryId']; 
echo "<option>Select Category</option>"; 
$res= mysql_query("select * from ".SUBCATEGORY." WHERE category_id = $categoryId order by id asc"); 
     while($data=mysql_fetch_array($res)) 
     { 
     echo "<option value='".$data['id']."'>."$data['sub_name']."</option>"; 
     } 
?> 

這將工作

+0

非常感謝你。它真的工作並幫助了我很多 – Shaddy 2014-10-08 10:55:22

+0

我的樂趣:) .. – 2014-10-08 11:42:02

0

您查詢是錯誤的,從表名稱中刪除quete

$res= mysql_query("select * from ".CATEGORY." order by id asc"); 
$res= mysql_query("select * from ".SUBCATEGORY." order by id asc"); 

這樣

$res= mysql_query("select * from CATEGORY order by id asc"); 
$res= mysql_query("select * from SUBCATEGORY order by id asc"); 

在我看來,你可能公頃在第二個查詢中添加where子句。

0

嘗試這可能會幫助你......

<?php 
if($_GET['id']) 
{ 
    $id=$_GET['id']; 
} 
?>       
<script> 
function newDoc(str) 
{ 
    window.location.assign("your_page_name.php?id="+str) 
} 
</script> 

<div class="form-group"> 
    <label for="exampleInputEmail1">Category Name</label> 
    <select name="type" id="type" onchange="newDoc(this.value)"> 
     <option>Select Category</option> 

     <?php 
     $res= mysql_query("select * from ".CATEGORY." order by id asc"); 

     while($data=mysql_fetch_array($res)) 
     { 
     ?> 
      <option value="<?php echo $data['id'];?>"><?php echo $data['cat_name'];?></option> 
     <?php 
     } 
     ?> 

    </select> 
</div> 
<div class="form-group"> 
    <label for="exampleInputEmail1">Subcategory Name</label> 
    <select name="type1" id="type1"> 
     <option>Select Category</option> 
     <?php 
     $res= mysql_query("select * from ".SUBCATEGORY." where SUB_ID='$id' order by id asc"); 
     while($data=mysql_fetch_array($res)) 
     { 
     ?> 
     <option value="<?php echo $data['id'];?>"><?php echo $data['sub_name'];?></option> 
     <?php 
     } 
     ?> 
    </select> 
</div> 
相關問題