Welcome Guest, Not a member yet? Register   Sign In
Triple Drop Down
#1

I have 3 database.
I use this code,when i click category sub category drop down not populated.here is my code.
I want help

category : cat_id,category
Sub-category: sub_id,sub_name,cat_id
company:id,cat_id,sub_id, cname, cadd, cweb

model file :Cities_countries_model 


Code:
<?php  
class Cities_countries_model extends CI_Model {  
   public function __construct()  
   {  
      $this->load->database();  
   }  
   //fill your contry dropdown  
   function getcountry()
     {
        $this->db->select('*');
        $query = $this->db->get('category');
        return $query->result();
     }

      function getstate($cat_id='')
     {
        $this-> db->select('*');
        $this-> db->where('cat_id', $cat_id);
        $query = $this->db->get('sub_category');
        return $query->result();
     }
    
      function getcity($sub_id='')
     {
        $this -> db -> select('*');
        $this -> db -> where('sub_id', $sub_id);
        $query = $this -> db -> get('company');
        return $query->result();
     }
     
}

controllers:countries


Code:
<?php  
   class Countries extends CI_Controller {  
   public function __construct()  
   {  
      parent::__construct();  
      $this->load->database();  
      $this->load->helper('url');  
      $this->load->model('cities_countries_model');  
   }  
   public function index()
    {
        $this->load->helper('url');
        $this->load->model('cities_countries_model');
        $data['category'] = $this->cities_countries_model->getcountry();           
        $this->load->view('cascadeDrop',$data);   
    }
  

public function ajax_state_list($cat_id)
    {
        $this->load->helper('url');
        $this->load->model('cities_countries_model');
        $data['subcat'] = $this->cities_countries_model->getstate($cat_id);
        $this->load->view('cascadeDrop',$data);
    }
    
    public function ajax_city_list($sub_id)
    {
        $this->load->helper('url');
        $this->load->model('cities_countries_model');
        $data['company'] = $this->cities_countries_model->getcity($sub_id);
        $this->load->view('cascadeDrop',$data);
    }
}  


views:cascadeDrop


Code:
<html xmlns="http://www.w3.org/1999/xhtml">  
   <head>  
      <meta http-equiv="Content-Type" content="text/html;  
      charset=utf-8" />  
      <title>Category List</title> 
      <script type="text/javascript" src="C:\xampp\htdocs\codeigniter\assets\jquery\jquery-3.1.0.min.js"></script>    
      </head>  
   <body>  
      <table class="table table-bordered" width="650" border="1" align="center">
 <tr>
    <td><div align="center">Category</div></td>
    <td><div align="center">:</div></td>
    <td><select name="category_details" id="category_details" onChange="getcatdetails(this.value)">
        <option value="" selected="selected" >Select Category</option>
            <?php foreach($category as $count): ?>
                <option value="<?php echo $count->cat_id; ?>"><?php echo $count->category;?></option>
            <?php endforeach; ?> 
     </select></td>
  </tr>
    <tr>
    <td><div align="center">Sub Category</div></td>
    <td><div align="center">:</div></td>
    <td>
      <select name="select_subcat" id="old_cat" onChange="getsubcatdetails(this.value)">
            <option selected="selected">Select Subcategory</option>
            <?php foreach($subcat as $stt): ?>
                <option value="<?php echo $stt->sub_id;?>"><?php echo $stt->sub_name;?></option>
            <?php endforeach; ?> 
        </select></td>
    </tr>
    <tr>
    <td><div align="center">Company</div></td>
    <td><div align="center">:</div></td>
    <td>
      <select name="selectcity" id="old_subcat">
        <option selected="selected">Select Company</option>
        <?php foreach($company as $ct): ?>
            <option value="<?php echo $ct->id; ?>"><?php echo $ct->cname;?></option>
        <?php endforeach; ?> 
      </select></td>
  </tr> 
</table>
              

  <script>
        
            function getcatdetails(id)
            {
                //alert('this id value :'+id);
                $.ajax({
                    type: "POST",
                    url: '<?php echo site_url('ajax_state_list').'/';?>'+id,
                    data: id='cat_id',
                    success: function(data){
                        //alert(data);
                        $('#old_cat').html(data);
                                            },
                        });
            };
             
            </script>
            <script>
            function getsubcatdetails(id)
            {
               
                $.ajax({
                    type: "POST",
                    url: '<?php echo site_url('ajax_city_list').'/';?>'+id,
                    data: id='sub_id',
                    success: function(data){
                       
                        $('#old_subcat').html(data);
                },
});
            }
                </script>





   </body>  
</html>  
Reply
#2

(This post was last modified: 08-08-2016, 09:03 AM by PaulD.)

First, try echoing (with print_r) the array you are expecting on the page somewhere so you can see if it is actually populated or not. At least then you will be able to tell if the problem is in your js or your html or your php or your sql.

Just do this at the top of your view (after your body tag of course)

PHP Code:
<p><?php echo print_r('my_array');</p

May I say, with the greatest possible respect, your HTML is horrible :-)

Code:
<td><div align="center">:</div></td>

Try some css on the table data.
Reply
#3

Both your AJAX methods load the same view that was initially loaded for displaying the page:
PHP Code:
$this->load->view('cascadeDrop',$data); 
That's not the way to add or manipulate content on a page without a complete page refresh.
The AJAX method must echo the new output you need. In your case, just the (html) contents of a dropdown for selecting a state or city.
Try to build this html inside the controller first. If needed, you can put it in a view, and load it like this:
PHP Code:
echo $this->load->view('dropdown_cities',$data,TRUE);  //the 3rd parameter (TRUE) will return the output as a string. 
This will return only the html code for one of the dependent dropdowns.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB