Friday, 13 April 2012

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

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`)
)
 
 
 
<?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 
}?>
 
 
 
<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>  

No comments:

Post a Comment