Codeigniter:Jquery基于组合框填充文本框

时间:2015-08-12 03:20:32

标签: php jquery codeigniter combobox codeigniter-2

我是JQuery中的Nubie,特别是在ajax中... 我已阅读有关下拉组合框的文章(从数据库中读取)然后填写其他文本框(根据组合框数据从数据库中读取)但我感到困惑

我需要Jquery / Ajax /显示它的东西

有人可以解决我的问题并向我解释..

控制器

public function tambah()
{
    $cek = $this->session->userdata('logged_in');
    if(empty($cek))
    {
        header('location:'.base_url().'app_admin');
    }
    else
    {
        $d['nama_madrasah'] = "";
        $d['nsm'] = "";
        $d['provinsi'] = "";
        $d['alamat'] = "";
        $d['kecamatan'] = "";
        $d['kab_kota'] = "";
        $d['jenis_bantuan'] = "";
        $d['jenjang'] = "";
        $d['th_ang'] = "";
        $d['sumber_dana'] = "";
        $d['tahap_pencairan'] = "";
        $d['st'] = "tambah";

        $d['dt_nsm'] = $this->app_model->ShowNSMMaster();

        $this->load->view("app_admin/madrasah/input",$d);
    }
}

我的观点

<select data-placeholder="Cari NSM..." class="chzn-select" style="width:260px;" tabindex="2" name="nsm" id="dt_nsm"  >
              <?php
              if($st=='edit')
              {
              ?>
                <option value="<?php echo '$nsm'; ?>" selected="selected"><?php echo '$nsm'; ?></option>
              <?php
              }
              else
              {
              ?> 
              <option value="" selected="selected">- Pilih -</option>
                <?php
                foreach($dt_nsm->result_array() as $dp1)
                {
          ?>
                <option value="<?php echo $dp1['nsm']; ?>"><?php echo $dp1['nsm']; ?> | <?php echo $dp1['nama_madrasah']; ?></option>
                <?php
                }
            }
          ?>
              </select>
            </span></div>
            <label class="control-label" for="nama_madrasah">Nama Madrasah </label>
            <div class="controls">
              <input type="text" class="span" name="nama_madrasah" id="nama_madrasahx" value="<?php echo $nama_madrasah; ?>" placeholder="">
            </div>
            <label class="control-label" for="alamat">Alamat</label>
            <div class="controls">
              <input type="text" class="span" name="alamat" id="alamatx" value="<?php echo $alamat; ?>" placeholder="">
            </div>

JS

<script type="text/javascript">
    $('#dt_nsm').on('change', function(){
    dt_nsm = $('#dt_nsm option:selected').val(); // the dropdown item selected value
    $.ajax({
        type :'POST',
        dataType:'json',
        data : { dt_nsm : dt_nsm },
        url : 'getresult.php',
        success : function(result){
         $('#nama_madrasahx).val(result['nama_madrasahx']); 
         $('#kecamatanx).val(result['kecamatanx']);  
        }
    });});
    </script> 

getresult.php

<?php 
require_once ('../config/db.config.php');

$dt_nsm = $_POST['dt_nsm'];
$query = "SELECT * FROM bantuan WHERE nsm = '$nsm' ";

$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
echo json_encode($row);
?>

Form View

谢谢你的时间。需要建议!

1 个答案:

答案 0 :(得分:0)

更改

foreach($dt_nsm->result_array() as $dp1)

foreach($dt_nsm->result_array() as $key=>$dp1)