Php CodeIgniter:根据选择的第一个选择选项,尝试从数据库表加载选项值
我正在使用CodeIgniter,有一个带有2个选择选项的表单。第一个选择选项是汽车品牌,第二个选择选项是品牌。如果我从“BMW”中选择汽车品牌,则第二个选择选项中的值应更改并显示BMW制造的所有车型Php CodeIgniter:根据选择的第一个选择选项,尝试从数据库表加载选项值,php,ajax,codeigniter,codeigniter-3,Php,Ajax,Codeigniter,Codeigniter 3,我正在使用CodeIgniter,有一个带有2个选择选项的表单。第一个选择选项是汽车品牌,第二个选择选项是品牌。如果我从“BMW”中选择汽车品牌,则第二个选择选项中的值应更改并显示BMW制造的所有车型 **WelcometoDemoCar.php (View)** *//to get the Car Make List Box* <input type = "text" name = "car_list" list="car_dalalist" id = "car_list" clas
**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;
}
非常感谢你的时间和帮助
提前感谢。请更新数据:{value:'value'},数据:{value:value}
从value中删除单引号关于代码有几个问题。 未来参考:见OP帖子上的评论 主要问题是单击处理程序:
$("#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);
},
});
});
控制器和模型的问题
@你能分享你的ajax吗code@Abhijit我已经更新了我的ajax代码,但该车列表并未以表格形式出现。另外,get_数据不存在于您的代码中provided@Abhijit对不起,我的错。。元素名称和ID为car_list。其在模型和控制器中与car_列表相同。我现在将更新我的code@Abhijit我刚刚更新了问题中的代码。很抱歉我将其从value改为car_list,但仍然没有显示在下拉列表中。您能看到AJAX调用被触发,请求中为其发布了什么值吗?
$("#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);
},
});
});
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;
}
}