Smudger
Smudger

Reputation: 10791

model to fetch single result from db with jquery

I have a controller function which is called by jquery:

  function get_sku_prices(){
    $this->load->model('Sales_model');
    if (isset($_GET['term'])){
      $q = strtolower($_GET['term']);
      $this->Sales_model->get_sku_price($q);
    }
}

The model, get_sku_price is:

  function get_sku_price($q){
    $this->db->select('price');
    $this->db->where('sku', $q);
    $query = $this->db->get('products');
    if($query->num_rows > 0){
      foreach ($query->result_array() as $row){
        $row_set[] = htmlentities(stripslashes($row['price'])); //build an array
      }
      $this->output->set_content_type('application/json')->set_output(json_encode($row_set));

    }
  }

what I want, is to return the result of the above query(which is essentially select price from products where sku=36113) to the input variable 'price' on my view form.

My view syntax is:

<html>
   <head>
      <title>
         Capture blank Order
      </title>
      <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
      <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/jquery-ui.min.js"></script> 
      <link rel="stylesheet" href="<?php echo base_url() ?>css/sales.css" />

<script type="text/javascript"> 
 function callAutocomplete(element) 
 { 
     $(element).autocomplete( 
     { 
         source: "sales/get_sku_codes", 
         messages: 
         { 
             noResults: '', 
             results: function() {} 
         }, 
         select: function( event, ui ) 
         {
             var selectedObj = ui.item; 
             $.post('sales/get_sku_prices', {data:selectedObj.value},function(result) { 
             $(ui).parent('tr').find('input[id^="pricepercube"]').val(result); 
             }); 
         } 
     }); 
 } 

 $(function() 
 { 
     var counter = 1; 
     jQuery("table.authors-list").on('change','input[name^="qty"]',function(event) 
     { 
         event.preventDefault(); 
         counter++; 
         var newRow = jQuery('<tr>'+ 
         ' <td><a class="deleteRow"> x </a></td>' + 
         ' <td><input type="text" id="product' + counter + '" name="product' + counter + '" /></td>' + 
         ' <td><input type="text" id="qty' + counter + '" name="qty' + counter + '" /></td>'+ 
         ' <td><input type="text" id="price' + counter + '" name="price' + counter + '" /></td>'+ 
         ' <td><input type="text" id="discount' + counter + '" name="discount' + counter + '"  /></td>'+ 
         ' <td valign=top><input type="checkbox" id="treated' + counter + '" name="treated' + counter + '" /></td>'+ 
         ' </tr>'); 
         jQuery('table.authors-list').append(newRow); 
         callAutocomplete("#product"+ counter); 
     }); 

$("#product").autocomplete( 
 { 
 source: "sales/get_sku_codes", 
 messages: 
 { 
 noResults: '', 
 results: function() {} 
 }, 
 select: function( event, ui ) 
 { 
 var selectedObj = ui.item; 
 $.post('<?=site_url("sales/get_sku_prices")?>', {data:selectedObj.value},function(result) 

 { 
 $("#price").val(result); 
 }); 
 } 
 });

 }); 

 </script>


  </head>
<body>

<table class="authors-list" border=0>
  <tr><td></td><td>Product</td><td>Qty</td><td>Price/Cube</td><td>Discount</td><td>treated</td></tr>
  <tr>
   <td><a class="deleteRow"> x </a></td>
   <td><input type="text" id="product" name="product" /></td>
   <td><input type="text" id="qty" name="qty" /></td>
   <td><input type="text" id="price" name="price" /></td>
   <td><input type="text" id="discount" name="discount" /></td>
   <td valign="top" ><input type="checkbox" id="treated" name="treated" /></td>
</tr>
</table>

</body>
</html>

Firefox returns a 200 OK message. you can see the post information contains the contents of the product input, but the returned HTML is blank?

UPDATE

mysql enter image description here

post enter image description here

blank html enter image description here

is my model query correct? is the post being correctly passed tot he model? how can I verify this?

Thanks again.

Upvotes: 3

Views: 759

Answers (2)

Jeemusu
Jeemusu

Reputation: 10533

You should be outputing your data from the controller, not the model.

Controller:

function get_sku_prices(){
    $this->load->model('Sales_model');
    if (isset($_GET['term'])){
        $q = strtolower($_GET['term']);
        $data = $this->Sales_model->get_sku_price($q);
        $this->output->set_content_type('application/json')->set_output(json_encode(data));
    } 
}

Model:

 function get_sku_price($q){
    $this->db->select('price');
    $this->db->where('sku', $q);
    $query = $this->db->get('products');
    if($query->num_rows > 0){
        foreach ($query->result_array() as $row){
            $row_set[] = htmlentities(stripslashes($row['price'])); //build an array
       }
       return $row_set;
    }
  }

Further to the above, you are passing your data via AJAX as POST data, yet in your controller you are trying to receive data from the GET array.

Upvotes: 0

Dipesh Parmar
Dipesh Parmar

Reputation: 27364

You are passing data inside $.post and getting it as term.

Change

if (isset($_GET['term'])){

to

if (isset($_POST['data'])){

Upvotes: 2

Related Questions