简体   繁体   English

如何一键传递一个列表框值到另一个列表框

[英]How to pass one listbox value to other listbox with one click

I am building a online store application by using php and mysql. 我正在使用php和mysql构建在线商店应用程序。 I made a list box which can be used to list out the main categories.. once we select the main categories, the sub categories should comes down. 我做了一个列表框,可以用来列出主要类别。.一旦我们选择了主要类别,子类别就会出现。 How can we accomplished this task without button press or page reload. 我们如何在不按按钮或不重新加载页面的情况下完成此任务。

the code for list box 列表框的代码

<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>

and the table design is 桌子的设计是

id categories_name parent_id categories_sort id Categories_name parent_id Categories_sort

plz help me 请帮我

Thanks 谢谢

Try this: Jquery + Ajax 试试这个: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);
                }
            }
        });

    })

I suggest you implement the onchange event on the first listbox so that it populates the other listbox with the subcategories for this category: 我建议您在第一个列表框上实现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>

in js: 在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);
                } 
            }
        });
}

in process.php: 在process.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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM