I have a form that contains an input using select option (with select2) that displays data when the user inputs several characters first (server side rpocessing). For now, the results of data from server side have been successfully added to the value select. But I want the results from the server side also appear on other inputs (input id NAMA and JABATAN). here's my code:
it's HTML elements
/* This is option input */
<select class="form-control select2" id="karyawan0" name="nip">
<option value="">Type 5 or more character</option>
</select>
/* Those are hidden inputs to save other value */
<input type="hidden" name="nama" id="nama">
<input type="hidden" name="jabatan" id="jabatan">
it's Javascript function when typing on select option (runs server side processing)
$("#karyawan0").select2({
allowClear: true,
placeholder: 'Masukkan Nama/NIP Karyawan',
ajax: {
dataType: 'json',
url: '<?php echo base_url();?>isian/get_karyawan',
delay: 800,
data: function(params) {
return {
search: params.term
}
},
processResults: function (data, page) {
return {
results: data
};
},
},
escapeMarkup: function(markup) {
return markup;
}, // let our custom formatter work
minimumInputLength: 5
});
here's my PHP function (I use CodeIgniter)
function get_karyawan(){
$term = trim(strip_tags($_GET['search'])); //<-- it's terms value
//here's my datasource
$get_karyawan = $this->global->get_karyawan($term);
$list = array();
$key=0;
IF($get_karyawan){
foreach($get_karyawan as $row){
$list[$key]['id'] = $row->NIP; // --> it returns to value of NIP
$list[$key]['text'] = $row->NIP." - ".$row->NAMA; // --> it returns to view of NIP
$list[$key]['nama'] = $row->NAMA; // --> it must be return to value of NAMA (hidden input)
$list[$key]['jabatan'] = $row->POSISI; // --> it must be return to value of JABATAN (hidden input)
$key++;
}
echo json_encode($list);
} else {
//when empty
echo "empty bruh";
}
}
that's all I ask, if you still dont get it what I want, kindly comment.. thanks for your attention and kind help :)