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 カテゴリ名 親 ID カテゴリ_並べ替え

助けてください

ありがとう

4

2 に答える 2

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);
                } 
            }
        });
}

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>
于 2013-03-19T09:38:01.517 に答える
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);
                }
            }
        });

    })
于 2013-03-19T07:37:02.910 に答える