2013-03-19 70 views
0

我正在使用php和mysql構建在線商店應用程序。我製作了一個列表框,可以用來列出主要類別。一旦我們選擇了主要類別,子類別就應該下來。我們如何在沒有按下按鈕或頁面重新加載的情況下完成此任務。如何通過一次點擊將一個列表框值傳遞給其他列表框

的列表框中

<select name="category" id="inputarealistproduct" onfocus="getsubcat();"> 
     <option> Select category</option> 
    <?php 
     $query="select id, categories_name from categories where parent_id=0"; 
     $result=mysql_query($query); 
     while(list($id, $name)=mysql_fetch_row($result)) 
     { 
      echo "<option value=\"".$id."\">".$name."</option>";    
     } 
    ?></select> 

和表的設計代碼

ID categories_name PARENT_ID categories_sort

plz幫助我

感謝

+1

這是第一個列表框嗎? – 2013-03-19 07:32:34

+0

是的,它是主要類別的列表框.. – loginnet87 2013-03-19 08:36:39

+0

查看答案...... – 2013-03-19 08:43:35

回答

0

試試這個:jQuery的+ Ajax

$('#inputarealistproduct').change(function() { 
     var product = $('#inputarealistproduct').val(); 
     $.ajax({ 
      url : 'url of your page where you ececute query', 
      type : 'GET', 
      data : { 
       item : product 
      }, 
      success : function (resp) { 
       if (resp.status) { 
        // manipulate your response data here. 
       } else { 
        show_message('error', resp.msg); 
       } 
      } 
     }); 

    }) 
0

我建議你實現第一個列表框onchange事件,以便它填充其他列表框與子類別的該類別:

<select name="category" id="inputarealistproduct" onfocus="getsubcat();" onchange="GetSubCategories()"> 
     <option> Select category</option> 
    <?php 
     $query="select id, categories_name from categories where parent_id=0"; 
     $result=mysql_query($query); 
     while(list($id, $name)=mysql_fetch_row($result)) 
     { 
      echo "<option value=\"".$id."\">".$name."</option>";    
     } 
    ?></select> 
<div id="result"></result> 
在JS

function GetSubCategories(){ 
    var categoryId = document.getElementById("inputarealistproduct").value; 

    $.ajax({ 
      url : 'process.php', 
      type : 'GET', 
      data : "category=" + categoryId , 
      success : function (resp) { 
       if (resp.status) { 
        $("#result").html(data); 
       } 
      } 
     }); 
} 

過程.php:

<?php 
$category = $_GET["category"]; 
// change the query so that the column names match your subcategory table 
$query="select id, sub_categories_name from subcategories where category_id=". $category; 
$result=mysql_query($query); 
?> 

<select name="subcategory" id="subcategory"> 
      <option>Select sub category</option> 
     <?php while(list($id, $name)=mysql_fetch_row($result)) 
      { 
       echo "<option value=\"".$id."\">".$name."</option>";    
      } 
     ?> 
</select> 
+0

親愛的納達Naoushi,代碼是不是我的工作..我沒有得到選定的指數的價值,這意味着類別的價值 – loginnet87 2013-03-19 10:19:40

+0

你會得到什麼?錯誤? – 2013-03-19 10:28:52

+0

我已經編輯了我的帖子,現在類別ID是通過這個方法得到的:var categoryId = document.getElementById(「inputarealistproduct」).value; – 2013-03-19 10:36:45

相關問題