Php 更改Select选项后从数据库获取相应数据
我正在用codeigniter框架开发一个系统,但我遇到了一个障碍。我花了两天时间试图在文本框中显示从数据库填充的所选项目的相应数据 来自视图的代码 来自模型的代码 剧本 我这里的问题是更改select选项后的。数据被传递到控制器,但是我设置为post变量的任何变量都返回null。我甚至尝试将它设置为flashdata变量,但它也返回null。我只是不知道我做错了什么。我也愿意接受任何其他选择,以将所选选项的相应数据获取到文本框中。Php 更改Select选项后从数据库获取相应数据,php,jquery,ajax,codeigniter,Php,Jquery,Ajax,Codeigniter,我正在用codeigniter框架开发一个系统,但我遇到了一个障碍。我花了两天时间试图在文本框中显示从数据库填充的所选项目的相应数据 来自视图的代码 来自模型的代码 剧本 我这里的问题是更改select选项后的。数据被传递到控制器,但是我设置为post变量的任何变量都返回null。我甚至尝试将它设置为flashdata变量,但它也返回null。我只是不知道我做错了什么。我也愿意接受任何其他选择,以将所选选项的相应数据获取到文本框中。 提前感谢。您收到的数据有误。以下是错误的 $data['id
提前感谢。您收到的数据有误。以下是错误的
$data['id'] = $this->input->post('item_goods');
它将如下所示
$data['id'] = $this->input->post('id');
因为您是通过ajax的id
发送数据,而不是作为items\u goods
但是,ajax中的
url
也可以改进您必须将-$this->interface\u model->get\u item\u data\u by\u id(..)的返回值设置为$data元素
更改:
$this->interface_model->get_item_data_by_id($data['item_id']);
至:
$data['item_data'] = $this->interface_model->get_item_data_by_id($data['item_id']);
public function new_sale(){
$data['title']='Add sale';
$data['id']=$this->input->post('item_goods');
$data['items']=$this->interface_model->get_item_data();
$data['item_id']=$this->input->post('items_goods');
$this->session->set_flashdata('posted_id',$data['id']);
$data['item_data'] = $this->interface_model->get_item_data_by_id($data['item_id']);
$this->load->view('templates/header_interfaces');
$this->load->view('interfaces/new_sale', $data);
$this->load->view('templates/footer_interfaces');
}
$row = $query->row_array();
return isset($row['id'])?$row:NULL;
public function get_item_data_by_id($item_id){
$this->db->select('*');
$this->db->from('stock');
$this ->db->where('id',$item_id);
$query = $this->db->get();
$row = $query->row_array();
return isset($row['id'])?$row:NULL;
}
更新控制器功能:
$data['item_data'] = $this->interface_model->get_item_data_by_id($data['item_id']);
public function new_sale(){
$data['title']='Add sale';
$data['id']=$this->input->post('item_goods');
$data['items']=$this->interface_model->get_item_data();
$data['item_id']=$this->input->post('items_goods');
$this->session->set_flashdata('posted_id',$data['id']);
$data['item_data'] = $this->interface_model->get_item_data_by_id($data['item_id']);
$this->load->view('templates/header_interfaces');
$this->load->view('interfaces/new_sale', $data);
$this->load->view('templates/footer_interfaces');
}
$row = $query->row_array();
return isset($row['id'])?$row:NULL;
public function get_item_data_by_id($item_id){
$this->db->select('*');
$this->db->from('stock');
$this ->db->where('id',$item_id);
$query = $this->db->get();
$row = $query->row_array();
return isset($row['id'])?$row:NULL;
}
您还必须从模型返回结果记录
在底部插话:
$data['item_data'] = $this->interface_model->get_item_data_by_id($data['item_id']);
public function new_sale(){
$data['title']='Add sale';
$data['id']=$this->input->post('item_goods');
$data['items']=$this->interface_model->get_item_data();
$data['item_id']=$this->input->post('items_goods');
$this->session->set_flashdata('posted_id',$data['id']);
$data['item_data'] = $this->interface_model->get_item_data_by_id($data['item_id']);
$this->load->view('templates/header_interfaces');
$this->load->view('interfaces/new_sale', $data);
$this->load->view('templates/footer_interfaces');
}
$row = $query->row_array();
return isset($row['id'])?$row:NULL;
public function get_item_data_by_id($item_id){
$this->db->select('*');
$this->db->from('stock');
$this ->db->where('id',$item_id);
$query = $this->db->get();
$row = $query->row_array();
return isset($row['id'])?$row:NULL;
}
更新型号功能:
$data['item_data'] = $this->interface_model->get_item_data_by_id($data['item_id']);
public function new_sale(){
$data['title']='Add sale';
$data['id']=$this->input->post('item_goods');
$data['items']=$this->interface_model->get_item_data();
$data['item_id']=$this->input->post('items_goods');
$this->session->set_flashdata('posted_id',$data['id']);
$data['item_data'] = $this->interface_model->get_item_data_by_id($data['item_id']);
$this->load->view('templates/header_interfaces');
$this->load->view('interfaces/new_sale', $data);
$this->load->view('templates/footer_interfaces');
}
$row = $query->row_array();
return isset($row['id'])?$row:NULL;
public function get_item_data_by_id($item_id){
$this->db->select('*');
$this->db->from('stock');
$this ->db->where('id',$item_id);
$query = $this->db->get();
$row = $query->row_array();
return isset($row['id'])?$row:NULL;
}
在视图文件中,您可以通过变量-$item_data访问新项目数据。在阅读了一些线程和文档后,我能够获得所选选项的相应数据,并通过更改脚本将其显示在文本框中,在控制器中创建一个新函数,以接收post并将其传递给模型-在这里我也做了一些调整
以下代码是我先前发布的代码的更改和调整
控制器代码
public function new_sale(){
$data['title']='Add sale';
$data['id']=$this->input->post('items_goods');
$data['items']=$this->interface_model->get_item_data();
$this->load->view('templates/header_interfaces');
$this->load->view('interfaces/new_sale', $data);
$this->load->view('templates/footer_interfaces');}
public function get_item_data_by_id($item_id){
$response=array();
if($item_id['items_goods']){
$this->db->select('*');
$this ->db->where('id',$item_id['items_goods']);
$query = $this->db->get('stock');
$response = $query->result_array();
}
return $response;}
$('#items_goods').change(function(){
var item_name= $('#items_goods').val();
alert('here'+' '+items_goods);
var url = baseURL+'interfaces/get_item_details';
$.ajax({
url: url,
data: {'items_goods':items_goods},
type: 'POST',
cache: 'false',
dataType: 'json',
success: function(response){
var len = response.length;
if(len > 0){
var qty_unit= response[0].qty_unit;
var price_p_u= response[0].price_p_u;
$('#qty_unit').val(qty_unit);
$('#price_p_u').val(price_p_u);
}else{
$('#qty_unit').val('N/A');
$('#price_p_u').val('N/A');
}
}
});});
public function get_item_details(){
$item_id=$this->input->post();
$this->load->model('interface_model');
$item_data=$this->interface_model->get_item_data_by_id($item_id);
echo json_encode($item_data);
}
型号代码
public function new_sale(){
$data['title']='Add sale';
$data['id']=$this->input->post('items_goods');
$data['items']=$this->interface_model->get_item_data();
$this->load->view('templates/header_interfaces');
$this->load->view('interfaces/new_sale', $data);
$this->load->view('templates/footer_interfaces');}
public function get_item_data_by_id($item_id){
$response=array();
if($item_id['items_goods']){
$this->db->select('*');
$this ->db->where('id',$item_id['items_goods']);
$query = $this->db->get('stock');
$response = $query->result_array();
}
return $response;}
$('#items_goods').change(function(){
var item_name= $('#items_goods').val();
alert('here'+' '+items_goods);
var url = baseURL+'interfaces/get_item_details';
$.ajax({
url: url,
data: {'items_goods':items_goods},
type: 'POST',
cache: 'false',
dataType: 'json',
success: function(response){
var len = response.length;
if(len > 0){
var qty_unit= response[0].qty_unit;
var price_p_u= response[0].price_p_u;
$('#qty_unit').val(qty_unit);
$('#price_p_u').val(price_p_u);
}else{
$('#qty_unit').val('N/A');
$('#price_p_u').val('N/A');
}
}
});});
脚本代码
public function new_sale(){
$data['title']='Add sale';
$data['id']=$this->input->post('items_goods');
$data['items']=$this->interface_model->get_item_data();
$this->load->view('templates/header_interfaces');
$this->load->view('interfaces/new_sale', $data);
$this->load->view('templates/footer_interfaces');}
public function get_item_data_by_id($item_id){
$response=array();
if($item_id['items_goods']){
$this->db->select('*');
$this ->db->where('id',$item_id['items_goods']);
$query = $this->db->get('stock');
$response = $query->result_array();
}
return $response;}
$('#items_goods').change(function(){
var item_name= $('#items_goods').val();
alert('here'+' '+items_goods);
var url = baseURL+'interfaces/get_item_details';
$.ajax({
url: url,
data: {'items_goods':items_goods},
type: 'POST',
cache: 'false',
dataType: 'json',
success: function(response){
var len = response.length;
if(len > 0){
var qty_unit= response[0].qty_unit;
var price_p_u= response[0].price_p_u;
$('#qty_unit').val(qty_unit);
$('#price_p_u').val(price_p_u);
}else{
$('#qty_unit').val('N/A');
$('#price_p_u').val('N/A');
}
}
});});
NB:我在我的页脚中设置变量var baseURL=”“
,该变量包含在我的所有页面中
感谢
@Karlo Kokkak
和@rejonaul Alam
尝试帮助我解决url问题当我使用baseurl()+controller\u name/方法时,我收到一个404错误。当数据未传递时,我最初使用$data['id']。我在发帖时没有删掉那部分。。但这也不起作用。$data['id']
不是问题,问题是$this->input->post('item_-goods')代码>。它将是$this->input->post('id')代码>URL也将是站点\u URL(“控制器/方法”)
@mcprilla79@mcprilla79我还可以看到return$query模型中的代码>错误,将返回$query->row_array()代码>