CodeIgniter:尝试根据第一个选择选项的选择从数据库表中加载选项值



我正在使用CodeIgniter,并且有一个包含2个选择选项的表单。第一个选择选项是汽车品牌,第二个选择选项是品牌。如果我从"宝马"中选择汽车品牌,则第二个选择选项中的值应更改并显示宝马制造的所有车型。

**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;
}

我尝试了一些使用 ajax 在各种站点上发布的解决方案,但我认为我的值没有发布到控制器。

阿贾克斯代码

$("#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); 
}, 
});
});

非常感谢您的时间和帮助。

提前谢谢。

关于代码有几个问题。
供将来参考:请参阅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); 
}, 
});
});

控制器和模型
的问题

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'},( 从值中删除单引号 (

相关内容

  • 没有找到相关文章

最新更新