Reputation: 370
I am using CodeIgniter and have a form with 2 select options. First select options is the Car Make and the second select option is the Make. If I select the Car Make from the As 'BMW' the Values in the second select options should change and show all the Models Made by BMW.
**WelcometoDemoCar.php (View)**
*//to get the Car Make List Box*
<input type = "text" name = "car_list" list="car_dalalist" id = "car_list" class = "inktext inklarge" placeholder = "Type of Car" required = "" autocomplete="off" />
<datalist id="car_dalalist">
<?php foreach($carlist as $row_carlist){?>
<?php //echo $row_carlist->Make . " " .$row_carlist->Model ." " .$row_carlist->Year ;?>
<option value="<?php echo $row_carlist->Make;?>"> <?php echo $row_carlist->Make;?></option>
<?php }?>
</datalist>
*//to get the value in the Make Select List Box*
<input type = "text" name = "car_model" list="car_model_dalalist" id = "car_model" class = "inktext inklarge" placeholder = "Car Model" required = <datalist id="car_model_dalalist">
<?php foreach($carModel as $row_carModel){?>
<?php //echo $row_carlist->Make . " " .$row_carlist->Model ." " .$row_carlist->Year ;?>
<option value="<?php echo $row_carModel->Model;?>"><?php echo $row_carModel->Model;?> </option>
<?php }?>
</datalist>
**Welcome.php (Controller)**
$this->data['carlist'] = $this->PostModel->getCarDetails();
$this->data['carModel'] = $this->PostModel->getCarModel();
**PostModel.php (Model)**
*//to get car make*
function getCarDetails(){
$this->db->Distinct();
$this->db->select("Make");
$this->db->from('carlist');
$carListquery = $this->db->get();
return $carListquery->result();
}
*// to get car model*
function getCarModel(){
$make = $this->input->post('car_list');
$this->db->Distinct();
$this->db->select("Model");
$this->db->from('carlist');
$this->db->where('Make' . $make);
$carmodelquery = $this->db->get();
return $carmodelquery->result();
}
public function get_data()
{
$value = $this->input->post("value");
$data = $this->PostModel->get_data($value);
$option ="";
foreach($data as $d)
{
$option .= "<option value='".$d->id."' >".$d->Model."</option>";
}
echo $option;
}
I tried few solutions posted on various sites using ajax, but I think my values are not getting posted to the controller.
ajax code
$("#car_list").on("change",function(){
var value = $(this).val();
$.ajax({ url : "welcome/get_data",
type: "post",
data: {"value":'value'},
success : function(data){
$("#car_model").html(data);
},
});
});
Really appreciate your time and help.
Thank in advance.
Upvotes: 0
Views: 641
Reputation: 14191
There were a couple of issues regarding the code.
For future reference: see the comments on the OP's post
Main issue was with the click handler:
$("#car_list").on("change",function(){
var value = $(this).val();
$.ajax({ url : "welcome/get_data",
type: "post",
data: {"value":value}, //OP originally used single quotes on the value therefore passing a string instead of the actualy variable
success : function(data){
$("#car_model").html(data);
},
});
});
Issues with the controller and model
public function get_data()
{
$data = $this->PostModel->get_data(); //OP originally passed $value to the model but $value does not exist
$option ="";
if(count($data) > 0){
foreach($data as $d)
{
$option .= "<option value='".$d->Model."' >".$d->Model."</option>";
}
echo $option;
}
}
Upvotes: 1
Reputation: 152
Please update data: {"value":'value'},
with data: {"value":value}
( Remove single quotes from value )
Upvotes: 0