2015-12-11 57 views
0

我從penjualan_resep模塊該控制器如何在cibonfire中加載json?

public function load_data_mahasiswa() { 
$term = $_POST['parent_id']; 
$response = array(); 
$mahasiswa = $this->mahasiswa_model->find_by('nim', $term); //load data from mahasiswa model 
if ($mahasiswa) { 
    $response[] = $mahasiswa; 
}else{ 
    $response['error'] = 'Data Kosong'; 
} 
echo json_encode($response); //convert to json 
} 

,這是我的看法

<script language="javascript"> 
$(document).ready(function() { 
    $("#nim").keyup(function() { 
     var nisp = $('#nim').val(); 
     $.post('<?php echo site_url(SITE_AREA.'/content/penjualan_resep/load_data_mahasiswa');?>', //load data using json 
       {parent_id: nisp}, 
     function(data) { 
      $('#nama_pasien').val(data[0].nama); //load from database 
     }, 'json' 
       ); 
    }); 
}); 

<table> 
         <tr> 
          <td>NIM</td> 
          <td colspan="4"><input type="text" name="nim" id="nim"></td> 
         </tr> 
         <tr> 
          <td>Nama</td> 
          <td colspan="4"><input type="text" name="nama" id="nama_pasien"></td> 
         </tr> 
</table> 

我想自動填寫表單 「那抹」,當我填寫表格「NIM 「使用json,但它不能?如何解決它?我使用的是Cibonfire框架(基於codeigniter)。

+0

你得到的錯誤是什麼?當我填充「NIM」形式 –

+0

JSON不能加載。 cibonfire不承認$ term = $ _POST ['parent_id']; ? – Gusan

+0

什麼是模型'$ mahasiswa'的價值? –

回答

0

你,因爲你正在使用$.post解析JSON。

var data1 = $.parseJSON(data); 
alert(data1[0].nama) 
$('#nama_pasien').val(data1[0].nama); //load from database