<select id="makes2" name="makes2" style="width:200px;" data-hint="" onchange="load_dropdown_content1($('#field2'), this.value)">
<?php
$sql = mysql_query("SELECT distinct make FROM advertisements");
while ($row = mysql_fetch_array($sql)){
echo "<option value='".$row['make']."'>" . $row['make'] . "</option>";
}
?>
</select>
here my values are hard coded to load values from mysql database
when dropdown onchange() function following javascript function will be called
here my controller compareVehicles --> get_dropdown_values1 function is called asynchronously
<script type="text/javascript">
function load_dropdown_content1(field_dropdown1, selected_value1){
var result = $.ajax({
'url' : '<?php echo site_url('compareVehicles/get_dropdown_values1'); ?>/'+selected_value1,
'async' : false
}).responseText;
field_dropdown1.replaceWith(result);
}
inside the controller my function as follows
function get_dropdown_values1($filter11){
$this->load->model('model_c/cmp_vehi_model');
$result1 = $this->cmp_vehi_model->get_options1($filter11);
foreach($result1 as $key1 => $value1){
$final1 = array('{$key1}' => $value1);
}
$this->load->helper('form');
echo "<select name=\"field2\" id=\"field2\" onchange=\"load_dropdown_contentad($('#field5'), this.value);\" onblur=\"load_dropdown_contentad($('#field5'), this.value);\" onfocus=\"load_dropdown_contentad($('#field5'), this.value);\" style=\"width:200px\" >";
foreach($result1 as $key1 => $value1){
echo"<option value='{$value1}'>{$value1}</option>";
}
echo "</select>";
}
model that is calling from controller as follows
function get_options1($filter11){
$query1 = $this->db->query("SELECT distinct modelnumber FROM advertisements WHERE make LIKE '".$filter11."'");
$result1 = $query1->result_array();
$x1 = array();
foreach($result1 as $i1){
array_push($x1, $i1['modelnumber']);
}
return $x1;
}
in my controller php code is echo to load ajax called dropdown
Please go to following link to see how it works
http://sep06.netne.net/online_portal/index.php/controller_c/compareVehicles/cmpVehicles
Sunday, September 1, 2013
PHP codigniter ajax call to load dropdown when another dropdown changes
8:18 PM
No comments
0 comments:
Post a Comment