duanlanqian9974 2018-10-25 10:55
浏览 47

选择2 Serverside返回值到多输入PHP CodeIgniter AJAX

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

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
    • ¥15 lammps拉伸应力应变曲线分析
    • ¥15 C++ 头文件/宏冲突问题解决
    • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
    • ¥50 安卓adb backup备份子用户应用数据失败
    • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
    • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
    • ¥30 python代码,帮调试,帮帮忙吧
    • ¥15 #MATLAB仿真#车辆换道路径规划
    • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建