0
我想知道爲什麼它不適合我。我遵循從這裏的指示:http://stackoverflow.com/questions/5418179/problem-with-jquery-getjson-retrieve-data-from-controller-code-igniter但也許我做錯了什麼。我正在嘗試製作腳本,以便在用戶從選擇菜單中選擇一個值時填充其餘字段。從控制器使用.getJSON()和CodeIgniter接收數據的問題
我的jQuery腳本是這樣的:
$(document).ready(function() {
$('#selprod-1').change(function() {
var id = $('#selprod-1 option:selected').val();
$.post(
'<?php echo $head['site_link'];?>faktura/pobierzProdukt/'+id,
function(data){
alert(data);
$('#pkwiu').val(data.product_pkwiu);
$('#netto').val(data.product_netto);
$('#vat').val(data.product_vat);
$('#brutto').val(data.product_brutto);
$('#jedn').val(data.product_jedn);
},
'json'
);
});
});
我的HTML形式如下:
<tr>
<td>1</td>
<td><?php echo form_dropdown('produkt[]',$lista,'','id="selprod-1"');?></td>
<td><?php echo form_input(array('name'=>'pkwiu','class'=>'short','readonly'=>'readonly'));?></td>
<td><?php echo form_input(array('name'=>'netto','class'=>'short','id'=>'netto','readonly'=>'readonly'));?></td>
<td><?php echo form_input(array('name'=>'vat','class'=>'mini','id'=>'vat','readonly'=>'readonly'));?></td>
<td><?php echo form_input(array('name'=>'brutto','class'=>'short','id'=>'brutto','readonly'=>'readonly'));?></td>
<td><?php echo form_input(array('name'=>'jedn','class'=>'mini','id'=>'jedn','readonly'=>'readonly'));?></td>
<td><?php echo form_input(array('name'=>'ilosc','class'=>'short','id'=>'ilosc'));?></td>
<td><?php echo form_input(array('name'=>'knetto','class'=>'short','id'=>'knetto','readonly'=>'readonly'));?></td>
<td><?php echo form_input(array('name'=>'kvat','class'=>'short','id'=>'kvat','readonly'=>'readonly'));?></td>
<td><?php echo form_input(array('name'=>'kbrutto','class'=>'short','id'=>'kbrutto','readonly'=>'readonly'));?></td> </tr>
我控制器功能(工作)看起來是這樣的:
public function pobierzProdukt($id)
{
$json = json_encode($this->Faktura_model->getProduct($id));
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 2027 05:00:00 GMT');
header('Content-type: application/json');
echo $json;
}
我的模型功能如下所示:
public function getProduct($id)
{
$q = 'SELECT product_vat, product_netto, product_brutto, product_jedn
FROM products
WHERE product_id="'.$id.'"';
$w = $this->db->query($q);
return json_encode($w->row_array());
}
烏爾編碼兩次,不知道是否與json_encode交易或沒有,但仍然$ JSON應不是json_encode – Val 2011-05-16 12:43:42
非常感謝。我沒有注意到它。 – sunpietro 2011-05-16 13:42:41
是否解決了這個問題? – Val 2011-05-16 14:39:59