[英]CodeIgniter: Trying to load the option values from database table based on the selection of first select option
I am using CodeIgniter and have a form with 2 select options. 我正在使用CodeIgniter,并且有一个带有2个选择选项的表单。 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.
如果从“ BMW”中选择“汽车制造商”,则第二个选择选项中的值将更改并显示所有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在各种站点上发布的解决方案,但是我认为我的值没有发布到控制器上。
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. 预先感谢。
There were a couple of issues regarding the code. 关于代码有几个问题。
For future reference: see the comments on the OP's post 供将来参考:请参阅OP上的评论
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;
}
}
请更新data: {"value":'value'},
并更新data: {"value":value}
(从value中删除单引号)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.