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>