Jun
30

Ajax Tutorial: Dynamic Loading of ComboBox using jQuery and Ajax in PHP



Here is a script for beginners to create a dynamic loading of dropdown. Simple solution which is created by jQuery.

PART TWO IS HERE

Table Structure

CREATE TABLE IF NOT EXISTS `ajax_categories` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `category` varchar(50) NOT NULL,
  `pid` int(11) NOT NULL,
  PRIMARY KEY (`id`)
)

get_child_categoires.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
if($_REQUEST)
{
	$id 	= $_REQUEST['parent_id'];
	$query = "select * from ajax_categories where pid = ".$id;
	$results = mysql_query( $query);?>
 
	<select name="sub_category"  id="sub_category_id">
	<option value="" selected="selected"></option>
	<?php
	while ($rows = mysql_fetch_assoc(@$results))
	{?>
		<option value="<?php echo $rows['category'];?>  ID=<?php echo $rows['id'];?>"><?php echo $rows['category'];?></option>
	<?php
	}?>
	</select>	
 
<?php	
}?>

HTML

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<div class="both">
	<h4>Select Category</h4>
	<select name="search_category"  id="search_category_id">
	<option value="" selected="selected"></option>
	<?php
	$query = "select * from ajax_categories where pid = 0";
	$results = mysql_query($query);
 
	while ($rows = mysql_fetch_assoc(@$results))
	{?>
		<option value="<?php echo $rows['id'];?>"><?php echo $rows['category'];?></option>
	<?php
	}?>
	</select>		
</div>
 
<div class="both">
	<h4 id="show_heading">Select Sub Category</h4>
	<div id="show_sub_categories" align="center">
		<img src="loader.gif" style="margin-top:8px; float:left" id="loader" alt="" />
	</div>
</div>

Add To Facebook Stumble This Digg This Add To Del.icio.us Add To Reddit Add To Yahoo Add To Twitter


Written by ZeeShaN

ZeeShaN RasooL is a web developer who loves to work in latest technologies to create more interactive dynamic and beautiful web pages.

  • absfrm

    thanks a lot.
    How can i get sub categories of child?
    ex: after select main cat , next combobox show me children’s of it.and how can i show children’s of selected child in combobox?

    please help me.thanks

    April 30 2012
    CommentsLike
    • venky

      hi ,thanks, i need to do styles for this select box, it possible?? any tutorial for tht??

      February 27 2012
      CommentsLike
      • Simon

        love it :D

        January 23 2012
        CommentsLike
        • Ladres

          Hi,
          Thank you so much, but I have to ask;

          If I use 2 tables in one SQL database, how can we coding?..
          For example;
          First combo will use table1 data (id1, categori1, pid1)
          Second combo will use table2 data (id2, categori2, pid2)

          How can we connect these two tables?

          Thank you so much.

          December 07 2011
          CommentsLike

          • great.. i found what i want.
            thanks alot

            November 19 2011
            CommentsLike
            • MFR khan

              Thanks Brother

              November 16 2011
              CommentsLike
              • ZeeShaN

                Thanks mark :)

                October 23 2011
                CommentsLike

                • Very nicely written..
                  Thank you so much for Sharing!

                  October 21 2011
                  CommentsLike

                  • Very Cool Article! I need to know how can increase sub selections? like Category -> Brand -> Prod -> Color like

                    October 06 2011
                    CommentsLike
                    • Nguyen Dai

                      thank you very much. I a new deverlop web.
                      knowledge is very litle. I need you help!
                      hay contac to my mail please!

                      October 04 2011
                      CommentsLike









                      Enter your Email:

                      Click Here for Popular

                      Who I Am

                      Zeeshan Rasool

                      Software Engineer - PHP
                      Lahore - Pakistan

                      zeeshan(@)99points.info
                      Skype: zeeshan-rasool
                      gTalk: zishan.rasool85

                      Categories

                      Tags

                      Comments