0

I want to populate textbox values automatically from DB based on dropdown selection.....I have tried this, but after selecting dropdown the page is refreshing but the values are not populated onto textbox....need Help!!

Table

Controller:

    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");
    }

Model:

    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();
    }

View:

 <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">
5
  • Replace data:'selection='+mainselection, with data:{'selection':mainselection}, Commented Nov 21, 2018 at 9:49
  • 1
    Replace $("#hqda").html(result); with $("#hqda").val(result); Commented Nov 21, 2018 at 9:50
  • @Twinkle I replaced it ..... no changes, it's working same way :( Commented Nov 21, 2018 at 9:53
  • @SrujanRamannaGowda see my answer below - you need to check what your ajax call is actually returning. Also check if it's even succeeding or not (use your browser's Developer Tools to do that). I don't know if your isrexpense() model accepts post requests, and it also seems that it potentially doesn't accept any input data - the method has no parameters. Commented Nov 21, 2018 at 9:56
  • @ADyson Ok sir, i'm bit new to ajax so i'm finding it difficult....i'll debug and get back to you Commented Nov 21, 2018 at 10:07

2 Answers 2

3

Controller :

            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));
            }

Model:

            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 '';
                }
            }

View :

            <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>
Sign up to request clarification or add additional context in comments.

4 Comments

Fatal error: Uncaught Error: Cannot use object of type stdClass as array in E:\xampp\htdocs\SFA_test\application\models\Isr_model.php:116 Stack trace: #0 E:\xampp\htdocs\SFA_test\application\controllers\Isr.php(86): Isr_model->test(false) #1 E:\xampp\htdocs\SFA_test\system\core\CodeIgniter.php(359): Isr->getValFromDb() #2 E:\xampp\htdocs\SFA_test\index.php(204): require_once('E:\\xampp\\htdocs...') #3 {main} thrown in E:\xampp\htdocs\SFA_test\application\models\Isr_model.php on line 116
Try $query->result_array(); instead of $query->result(); in model test function
have you got the solution or not ?
@Coolbuddy No i cleared the above mentioned error but not getting the result..... Nothings happening after selecting state from drop down
0

Textboxes don't have an "innerHTML" property. If you want to set the content of a textbox, you should set its value property. In jQuery you do this using the .val() method.

Replace

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

with

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

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

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

P.S. You may also want to take a look at what your AJAX call returns - it's not clear whether it would return the content you're looking for or not - it appears to return a whole HTML view, rather than just a single value to place into a textbox.

13 Comments

Oh i was not knowing that, i made the changes but still it works same...
@SrujanRamannaGowda see my comment on the main question. If nothing appears to be happening then you need to start debugging to see if the code is running as you expected, and what it is producing (if anything)
i tried debugging, In XHR i don't see any activity after selecting state dropdown....
in Ajax function i replaced .on with .live , now i see some activity in XHR but its not passing any parameter ,.... i think ajax call is fine now,but not passing any parameter with it
you replaced .on() with .live()? Why would you do that? .on() replaced .live() in jQuery 1.7, and now .live() is deprecated. It's like you're stepping back in time. Unless you have a very old version of jQuery you shouldn't need to do that. See api.jquery.com/live
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.