0

i have this controller from penjualan_resep module

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
}

and this is my view

<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>

i want to autofill form "Nama", when i fill form "NIM" using json, but it can't? how to solve it? i'm using cibonfire framework(base on codeigniter).

Gusan
  • 411
  • 3
  • 5
  • 19
  • what is the error you are getting?? – Niranjan N Raju Dec 11 '15 at 06:44
  • json can't load when i fill "NIM" form. Is cibonfire not recognize $term = $_POST['parent_id']; ? – Gusan Dec 11 '15 at 06:50
  • what is the value of `$mahasiswa` from model?? – Niranjan N Raju Dec 11 '15 at 06:54
  • this is my sql table CREATE TABLE IF NOT EXISTS `bf_mahasiswa` ( `id` int(11) NOT NULL AUTO_INCREMENT, `recid` varchar(30) NOT NULL, `kdsem` varchar(30) NOT NULL, `nim` varchar(30) NOT NULL, `tahun_masuk` year(4) NOT NULL, `nama` varchar(255) NOT NULL, `progdi` varchar(80) NOT NULL, `fakultas` varchar(80) NOT NULL, `fak_progdi` varchar(80) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=23901 ; – Gusan Dec 11 '15 at 07:03
  • can u add model code `find_by`?? – Niranjan N Raju Dec 11 '15 at 07:04
  • this is the value INSERT INTO `bf_mahasiswa` (`id`, `recid`, `kdsem`, `nim`, `tahun_masuk`, `nama`, `progdi`, `fakultas`, `fak_progdi`) VALUES (1, '651118', '20151', 'A210090004', 2009, 'ANANG CANDRA ADITYA PURYANTO', 'Pend. Akuntansi', 'KIP', 'KIP / Pend. Akuntansi'), (2, '629063', '20151', 'A210090021', 2009, 'PRIA TITIS WASKHITT0 AJI', 'Pend. Akuntansi', 'KIP', 'KIP / Pend. Akuntansi'), (3, '628644', '20151', 'A210090052', 2009, 'BUDIYANTO', 'Pend. Akuntansi', 'KIP', 'KIP / Pend. Akuntansi'), – Gusan Dec 11 '15 at 07:04
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/97585/discussion-between-niranjan-n-raju-and-debujang). – Niranjan N Raju Dec 11 '15 at 07:05

1 Answers1

0

You have to parse json since you are using $.post.

 var data1 = $.parseJSON(data);
 alert(data1[0].nama)
 $('#nama_pasien').val(data1[0].nama); //load from database
Niranjan N Raju
  • 12,047
  • 4
  • 22
  • 41