Jump to content

alter a three two tier cascade menu into a three tier


lional

Recommended Posts

Hi

here is my code for a two tier which I would like to make a three tier chained select

<script type="text/javascript">
        $(document).ready(function(){
            $("select#type").attr("disabled","disabled");
            $("select#category").change(function(){
            $("select#type").attr("disabled","disabled");
            $("select#type").html("<option>wait...</option>");
            var id = $("select#category option:selected").attr('value');
            $.post("select_type.php", {id:id}, function(data){
                $("select#type").removeAttr("disabled");
                $("select#type").html(data);
            });
        });
        /*
        $("form#select_form").submit(function(){
            var cat = $("select#category option:selected").attr('value');
            var type = $("select#type option:selected").attr('value');
            if(cat>0 && type>0)
            {
                var result = $("select#type option:selected").html();
                $("#result").html('your choice: '+result);
            }
            else
            {
                $("#result").html("you must choose two options!");
            }
            return false;
        });*/
        
    });
    </script>
<form id="select_form" name="select_form" action="cat_list.php" method="post">
            Choose a category:<br />
            <select id="category" name="category">
                <?php echo $opt->ShowCategory(); ?>
            </select>
</td><td>
 
           choose a type:<br />
            <select id="type" name="type">
                <option value="0">Choose a Sub-category</option>
            </select>
</td><td>
            <input type="submit" value="confirm" />

The select_type.php

<?php
include "includes/select.class.php";
echo $opt->ShowType();
?>

and select.class.php

<?php

class SelectList
{
    protected $conn;
 
        public function __construct()
        {
            $this->DbConnect();
        }
 
        protected function DbConnect()
        {
            include "includes/db_config.php";
             
            $this->conn = mysql_connect($host,$user,$password) OR die("Unable to connect to the database");
            mysql_select_db($db,$this->conn) OR die("can not select the database $db");
            return TRUE;
        }

        public function ShowCategory()
               {
                   $top_level_id = $_GET['catid'];
             $sql = "SELECT client_subcats.cat_id, categories.cat_id, categories.category FROM client_subcats, categories WHERE client_subcats.cat_id = categories.cat_id AND categories.top_level_id = '$top_level_id' GROUP BY categories.category ORDER BY categories.category";
            $res = mysql_query($sql,$this->conn);
            $category_out = '<option value="0">Choose a Category</option>';
            while($row = mysql_fetch_array($res))
            {
                $category_out .= '<option value="' . $row['cat_id'] . '">' . $row['category'] . '</option>';
            }
            return $category_out;
        }
 
        public function ShowType()
        {
           $sql = "SELECT client_subcats.subcat_id, subcats.subcat_id, subcats.subcat FROM client_subcats, subcats WHERE client_subcats.subcat_id = subcats.subcat_id AND subcats.cat_id = $_POST[id] GROUP BY subcats.subcat ORDER BY subcats.subcat";
            $res = mysql_query($sql,$this->conn);
            $type = '<option value="0">Choose a Sub-category</option>';
            while($row = mysql_fetch_array($res))
            {
                $type .= '<option value="' . $row['subcat_id'] . '">' . $row['subcat'] . '</option>';
            }
            return $type;
        }
}
 
$opt = new SelectList();

?>

This is a two tier and I would like to change it to a three tier.

I am very new to AJAX and any help would be appreciated

 

Thanks

 

Lional

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.