can anyone to help how create autofill form field in codeigniter 3 with AJAX, i have already browsing but no one success in progress
controller:
function autofill(){
$id_pemesan = $this->input->post('id_pemesan', TRUE);
$this->mod_marketing->autofill_by_id($id_pemesan,'pemesan');
$data = array(
'nama_depan' => nama_depan,
'nama_belakang' => nama_belakang,
'alamat' => alamat,
'provinsi_kota' => provinsi_kota,
'kode_pos' => kode_pos,
'email' => email,
'no_telp' => no_telp
);
echo json_encode($data);
}
model :
function autofill_by_id($id_pemesan,$table){
return $this->db->get_where($table,array('id_pemesan', $id_pemesan));
}
script:
<script>
$(function() {
$("#id_pemesan").change(function(){
var id_pemesan = $("#id_pemesan").val();
$.ajax({
url: '<?php echo site_url('marketing/autofill'); ?>',
type: 'POST',
dataType: 'json',
data: {
'id_pemesan': id_pemesan
},
success: function (pemesan) {
$("#nama_depan").val(pemesan.nama_depan);
$("#nama_belakang").val(pemesan.nama_belakang);
$("#alamat").val(pemesan.alamat);
$("#provinsi_kota").val(pemesan.provinsi_kota);
$("#kode_pos").val(pemesan.kode_pos);
$("#email").val(pemesan.email);
$("#no_telp").val(pemesan.no_telp);
}
});
});
});
that my latest try and its still fail
Change the controller so it checks if response should include data, or error. You actually were not passing the row to the array, so I'm guessing this is what you wanted:
Change the model to check for row:
Modify the JS so you can check for errors: