title: Asynchronously query the database by using ajax and jquery and post the result back in codeigniter View
body: <p>I've struggeled alot with this .</p>
  
  <p>I wanna send an ID in the CI model and get the returned value via CI controller</p>
  
  <p>My view is </p>
  
  <pre><code>      &lt;script type="text/javascript"&gt;
               function showsome(){
                   var rs = $("#s_t_item option:selected").val();
                   var controller = 'items';
                   var base_url = '&lt;?php echo site_url(); ?&gt;';
  
                   $.ajax({
                           url : base_url+ '/' + controller+ '/get_unit_item',
                       type:'POST',
                       contentType: 'json',
                       data: {item_id: rs},
                       success: function(output_string){
                           //$('#result_area').val(output_string);
                           alert(output_string);
                       }
                   });
  
               }
  
           &lt;/script&gt;
  </code></pre>
  
  <p>My Controller method is </p>
  
  <pre><code>    public function get_unit_item()
        {
          $received = $this-&gt;input-&gt;post('item_id');  
  
          $query = $this-&gt;units_model-&gt;get_unit_item($received);
          $output_string = '';
          if(!is_null($query)) {
          $output_string .= "{$query}";
          } else {
          $output_string = 'There are no unit found';
          }
          echo json_encode($output_string); 
  }
  </code></pre>
  
  <p>And my model function responsible </p>
  
  <pre><code>public function get_unit_item($where){
      $this-&gt;db-&gt;where('item_id',$where);
      $result = $this-&gt;db-&gt;get($this-&gt;tablename);
      if($result-&gt;num_rows()  &gt;0 ){
          $j = $result-&gt;row();
          return $j-&gt;unit_item_info ; 
      }
  }
  </code></pre>
  
  <p>Html codes </p>
  
  <pre><code>   &lt;?php $id = 'id="s_t_product" onChange="showsome();"';
          echo form_dropdown('product_id[]', $products, $prod,$id); ?&gt;
  </code></pre>
  
  <p>I tried to use the id only but failed to fire so passing a function onchange seems to pick the item and fire</p>
  
  <p>Using firebug I can see that the post request sends item_id=2 but the response length is 0 and with php result code 302</p>
  
  <p>POST 
  <img src="https://i.stack.imgur.com/2Y981.png" alt="enter image description here">
  RESPONSE
  <img src="https://i.stack.imgur.com/ssQMv.png" alt="enter image description here"></p>
  
  <p>How can I achive this?(The model is loaded on the contructor)</p>