Php 从自动完成建议中选择值后,根据所选值填写其余字段
因此,我在我的一个视图中有一个自动完成功能,正在工作,现在我想添加一个功能,用户在其中搜索产品,在一些关键字中写入找到它并选择它,当选择产品名称时,我想动态填写该产品的价格,信息在数据库中,我如何实现这一点 我的JQuery用于自动完成Php 从自动完成建议中选择值后,根据所选值填写其余字段,php,jquery,codeigniter,autocomplete,Php,Jquery,Codeigniter,Autocomplete,因此,我在我的一个视图中有一个自动完成功能,正在工作,现在我想添加一个功能,用户在其中搜索产品,在一些关键字中写入找到它并选择它,当选择产品名称时,我想动态填写该产品的价格,信息在数据库中,我如何实现这一点 我的JQuery用于自动完成 $(function(){ var controller_path = document.getElementById("get_controller_path").value; $("#product").autocomplete({
$(function(){
var controller_path = document.getElementById("get_controller_path").value;
$("#product").autocomplete({
source: controller_path
});
});
public function get_product(){
$this->load->model('offers_for_clients_model');
if (isset($_GET['term'])){
$q = strtolower($_GET['term']);
$this->offers_for_clients_model->get_product($q);
}
}
当选择自动完成建议时,我希望动态弹出价格的我的视图:
<td><input type="text" id="product" name="prodname"></td>
<input type="hidden" id="get_controller_path" value="<?echo base_url().'admin_site_offers_ctrl/get_product';?>">
<td><input style="width: 60px" type="text" name="price" id="price"></td>
自动完成功能的模型:
function get_product($q){
$this->db->select('*');
$this->db->like('nosauk_lv', $q);
$query = $this->db->get('produkti');
if($query->num_rows > 0){
foreach ($query->result_array() as $row){
$row_set[] = htmlspecialchars_decode(stripslashes($row['nosauk_lv'])); //build an array
}
echo json_encode($row_set); //format the array into json data
}
}
我应该如何处理这个问题?任何指向正确方向的指针都将是惊人的!谢谢
另外,自动完成功能非常好,不用担心。您可以这样尝试
$("#product").autocomplete({
source: function( request, response ) {
$.ajax({
url: customurl,
data: {term: request.term},
dataType: "json",
success: function( data ) {
response( $.map( data, function( item ) {
// you can set the label value
return {
label: item.value,
value: item.value,
}
}
}));
}
});
},
select: function( event, ui )
{
// Here again you can call ajax function to get the product price
var prodname=$('#prodname').val(ui.prodid);
getproductprice(prodname);
},
change: function( event, ui )
{
var prodname=$('#prodname').val(ui.prodid);
getproductprice(prodname);
}
});
<input type="hidden" name="prodname" id="prodname"> // Here you will get product name id after select the productname
请确保在您的模型中获得$row_set[]中的产品id
在html中声明如下
$("#product").autocomplete({
source: function( request, response ) {
$.ajax({
url: customurl,
data: {term: request.term},
dataType: "json",
success: function( data ) {
response( $.map( data, function( item ) {
// you can set the label value
return {
label: item.value,
value: item.value,
}
}
}));
}
});
},
select: function( event, ui )
{
// Here again you can call ajax function to get the product price
var prodname=$('#prodname').val(ui.prodid);
getproductprice(prodname);
},
change: function( event, ui )
{
var prodname=$('#prodname').val(ui.prodid);
getproductprice(prodname);
}
});
<input type="hidden" name="prodname" id="prodname"> // Here you will get product name id after select the productname
我认为这将有助于解决你的问题。谢谢很抱歉迟了回复,这对我来说很有效,但最终还是做了以下事情:
$(function(){
var controller_path = document.getElementById("get_controller_path").value;
$("#product").autocomplete({
source: controller_path, // ceļš uz kontrolieri kur atrodas metode, admin_site_offers_ctrl
select: function(a,b){
$(this).val(b.item.value); //grabed the selected value
getProductsOtherInfo(b.item.value);//passed that selected value
}
});
});
其他函数根据选定的值名称向数据库发出请求
并将它们加载到我需要的适当字段中,如下所示:
function getProductsOtherInfo(name){
var site_url = document.getElementById('get_products_other_info').value;
/*alert(site_url);*/
$.post(site_url, {
name : name,
site_url : site_url
},
function(rawdata){
var myObject = JSON.parse(rawdata);
$('#cena_pirms_atl').val(myObject[0].cena_ls);
$('#iepcena').html(myObject[0].ped_ien_ls);
$('#nom_id').val(myObject[0].ID);
getPZtotals();
});
}
控制员:
function get_products_other_info(){
$this->load->model('offers_for_clients_model');
$name = trim($_POST['name']);
$data['products_other_info'] = $this->offers_for_clients_model->get_products_other_info($name);
echo json_encode($data['products_other_info']);
}
模型:
function get_products_other_info($name){
$decode_name = htmlspecialchars(stripslashes($name));
$query = $this->db->where('nosauk_lv', $decode_name)->get('produkti')->result();
return $query;
}
视图:
尝试实施您的解决方案,完成后会返回给您,或者出现一些错误,不要担心您可以向上投票我的问题,我会稍后返回给您!:问题,将ID变量添加到该行集合的正确方法是什么,我目前正在努力解决这个问题,谢谢@本多弗。。在for循环中,您添加了此行“$row\u set[]=htmlspecialchars\u decodestripslashes$row['nosauk\u lv'];”;。在相同的下面,比如$row_set[]=$row['prodid']。因为行集合是数组。