[eluser]moriokasan[/eluser]
Hi all, I have a dropdown with providers and one with products. I want when I select a provider to have the second drop down with products from the selected provider.
Can anyone help me to populate the second dropdown ?
my view is:
Code:
<html>
<head>
<style type="text/css">
select {
background-color: #134553;
color: #ffffff;
width: 200px;
position: relative;
margin: 0 auto;
top: 50px;
font-size: 24px;
}
</style>
[removed]config->item('base_url');?>lib/jquery-1.6.1.min-dev.js">[removed]
[removed]
function refresh_products(){
var selected_provider = $('#providers option:selected').val();
$('#products').load("/test/dropdown/getproducts/777".replace("777", selected_provider));
alert(selected_provider);
}
$(document).ready(function(){
$('#providers').change(refresh_products);
});
[removed]
</head>
<body>
<h1> testing jquery dropdown in cascade</h1>
<input type="text" id="debug">Debug text</input><br></br>
<select id="providers" name="providers">
<?php if(isset($providers)) : foreach($providers as $provider) : ?>
<option value=<?php echo $provider->id; ?>>
<?php echo $provider->name; ?>
</option>
<?php endforeach; endif; ?>
</select>
<select id="products" name="products">
</select>
</body>
</html>
and the controller:
Code:
class Dropdown extends CI_Controller
{
function index()
{
//check session
$this->load->library('Mysession');
if ($this->mysession->isInactive($this)) return;
$this->load->model('crud2_model');
if($query2 = $this->crud2_model->select_all('select p.id, p.name from expenses_providers p order by p.name asc'))
{
$data['providers'] = $query2;
}
else
{
$data['providers'] = array();
}
$this->load->view('test/dropdown', $data);
}
function getproducts()
{
//check session
$this->load->library('Mysession');
if ($this->mysession->isInactive($this)) return;
$this->load->model('crud2_model');
if($query2 = $this->crud2_model->select_all('select p.id, p.name from expenses_products p where p.providerid = '.$this->uri->segment(4).' order by p.name asc'))
{
$data['products'] = $query2;
}
else
{
$data['products'] = array();
}
$results = $query2->result();
$returnValue = "";
foreach($results as $result)
{
$returnValue = $returnValue."<option value=".$result->id.">".$result->name."</option>";
}
return $returnValue;
}
}
The alert screen shows the correct selected value...
What happens is that when I select anything in the first drop down the second is populated with extremely many empty records. I am to tired to figure this one out so any help is appreciated. Thank you!