根据php中的下拉值将数据库值提取到多个文本框

时间:2018-11-21 09:46:15

标签: php ajax codeigniter

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

Table

控制器:

    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">

2 个答案:

答案 0 :(得分:3)

控制器:

            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>

答案 1 :(得分:0)

文本框没有“ innerHTML”属性。如果要设置文本框的内容,则应设置其 value 属性。在jQuery中,您可以使用.val()方法。

替换

$("#hqda").html(result); 

$("#hqda").val(result);

文档:https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input

http://api.jquery.com/val/

P.S。您可能还想看看AJAX调用返回的内容-不清楚是否会返回您要查找的内容-它似乎返回的是整个HTML视图,而不是返回一个单独的值一个文本框。