使用 AJAX 和 CodeIgniter 3 检索并显示值的总和
Retrieving and display a sum of values with AJAX and CodeIgniter 3
我不知道我使用的语法是否正确,但我想检索值的总和并将其显示在视图中。
控制器代码:
public function stockArticle()
{
$donnees_post = $this->input->post();
$this->load->model('batchquantity_model');
$stock = $this->batchquantity_model->getStockArticle($donnees_post);
echo json_encode($stock);
}
型号代码:
public function getStockArticle($donnees_post)
{
$reponse = [];
if(isset($donnees_post['idArticle']) && isset($donnees_post['idStorage']))
{
$this->db->select_sum('quantite');
$this->db->from('batchquantity');
$this->db->join('lots', 'batchquantity.idLot = lots.id');
$this->db->where('idArticle', $donnees_post['idArticle']);
$this->db->where('idStorage', $donnees_post['idStorage']);
$reponse = $this->db->get()->result_array();
}
return $reponse;
}
AJAX 在视图中获取代码:
$('#articles').change(function(){
var idArticle = $(this).val();
var idStorage = $('#storages').val();
$.ajax({
url:'<?=base_url()?>index.php/batchquantity/stockArticle',
method: 'post',
data: {idArticle: idArticle, idStorage: idStorage},
dataType: 'json',
success: function(response){
var len = response.length;
if(len > 0){
stock = response[0].quantite;
}
else
{
stock = 0;
}
$('#stock').val(stock);
}
});
});
我的代码会不会有错误,因为每次我在 jQuery 脚本
中启动 alert(response [0].quantite);
时都会出现空值
您可以像这样在模型中划行
$this->db->get()->row();
这样它将 return 单一结果,您可以像这样立即在 javascript 上显示它。
$('#articles').change(function(){
var idArticle = $(this).val();
var idStorage = $('#storages').val();
$.ajax({
url:'<?=base_url()?>index.php/batchquantity/stockArticle',
method: 'post',
data: {idArticle: idArticle, idStorage: idStorage},
dataType: 'json',
success: function(response){
if(response.length){
stock = response.quantite;
}
else
{
stock = 0;
}
$('#stock').val(stock);
}
});
});
我不知道我使用的语法是否正确,但我想检索值的总和并将其显示在视图中。
控制器代码:
public function stockArticle()
{
$donnees_post = $this->input->post();
$this->load->model('batchquantity_model');
$stock = $this->batchquantity_model->getStockArticle($donnees_post);
echo json_encode($stock);
}
型号代码:
public function getStockArticle($donnees_post)
{
$reponse = [];
if(isset($donnees_post['idArticle']) && isset($donnees_post['idStorage']))
{
$this->db->select_sum('quantite');
$this->db->from('batchquantity');
$this->db->join('lots', 'batchquantity.idLot = lots.id');
$this->db->where('idArticle', $donnees_post['idArticle']);
$this->db->where('idStorage', $donnees_post['idStorage']);
$reponse = $this->db->get()->result_array();
}
return $reponse;
}
AJAX 在视图中获取代码:
$('#articles').change(function(){
var idArticle = $(this).val();
var idStorage = $('#storages').val();
$.ajax({
url:'<?=base_url()?>index.php/batchquantity/stockArticle',
method: 'post',
data: {idArticle: idArticle, idStorage: idStorage},
dataType: 'json',
success: function(response){
var len = response.length;
if(len > 0){
stock = response[0].quantite;
}
else
{
stock = 0;
}
$('#stock').val(stock);
}
});
});
我的代码会不会有错误,因为每次我在 jQuery 脚本
中启动alert(response [0].quantite);
时都会出现空值
您可以像这样在模型中划行
$this->db->get()->row();
这样它将 return 单一结果,您可以像这样立即在 javascript 上显示它。
$('#articles').change(function(){
var idArticle = $(this).val();
var idStorage = $('#storages').val();
$.ajax({
url:'<?=base_url()?>index.php/batchquantity/stockArticle',
method: 'post',
data: {idArticle: idArticle, idStorage: idStorage},
dataType: 'json',
success: function(response){
if(response.length){
stock = response.quantite;
}
else
{
stock = 0;
}
$('#stock').val(stock);
}
});
});