首页 > 解决方案 > 根据php中的下拉值将数据库值获取到多个文本框

问题描述

我想根据下拉选择自动从数据库中填充文本框值.....我已经尝试过了,但是在选择下拉列表后页面正在刷新,但值没有填充到文本框......需要帮助!

桌子

控制器:

    public function isrexpense()
    {
    $data['query'] = $this->Isr_model->getstates();
    $data['names'] = $this->Isr_model->getAllNames();
    $data['query1'] = $this->Isr_model->test();
    $this->load->view("header"); 
    $this->load->view('Isr/isrexpense', $data);
    $this->load->view("footer");
    }

模型:

    function test()
    {
    $this->db->select('da_hq');
    $this->db->from('isr_policy_master');
    $this->db->where('state', $this->input->post('state'));
    $query = $this->db->get();
    return $query->result();
    }

看法:

 <select class="js-example-basic-single form-control">
 <?php 
 foreach($names as $row)
 { 
 echo '<option></option>';
 echo '<option value="'.$row->name.'">'.$row->name.'</option>';
 }
 ?>
 </select> 
 <select class="state form-control" id="state" name="state">
 <?php 
 foreach($query as $row)
 { 
 echo '<option value="'.$row->state_code.'">'.$row->state_name.'</option>';
 } ?>  
 </select>  


 <script>
 $('#state').on('change', function(){
 var mainselection = this.value; // get the selection value
 $.ajax({
 type: "POST",  // method of sending data
 url: "<?php echo site_url(); ?>/Isr/isrexpense",
 data:'selection='+mainselection,
 success: function(result)
 {
 $("#hqda").html(result);
  }
 });
 });
 </script>


 <input id="hqda" name="hqda" class="form-control" required type="number">

标签: phpajaxcodeigniter

解决方案


控制器 :

            public function isrexpense()
            {
                $data['query'] = $this->Isr_model->getstates();
                $data['names'] = $this->Isr_model->getAllNames();
                $this->load->view("header"); 
                $this->load->view('Isr/isrexpense', $data);
                $this->load->view("footer");
            }

            public function getValFromDb()
            {
                $state = $this->input->post('selection');
                $query1 = $this->Isr_model->test($state);
                echo json_encode(array('data'=>$query1));
            }

模型:

            function test($state)
            {
                $this->db->select('da_hq');
                $this->db->from('isr_policy_master');
                $this->db->where('state', $state);
                $query = $this->db->get();
                $result = $query->result();
                if(count($result)>0)
                {
                    return $result[0]['da_hq'];
                }
                else
                {
                    return '';
                }
            }

看法 :

            <script>
             $('#state').on('change', function(){
             var mainselection = this.value; // get the selection value
             $.ajax({
             type: "POST",  // method of sending data
             url: "<?php echo site_url(); ?>/Isr/getValFromDb",
             data:'selection='+mainselection,
             success: function(result)
             {
                console.log(result); // check this in your console and if require than use JSON.parse to get value
                $("#hqda").val(result.data);
              }
             });
             });
             </script>

推荐阅读