codeigniter - 使用 jquery 从数据库获取单个结果的模型

标签 codeigniter jquery model

我有一个由 jquery 调用的 Controller 函数:

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

模型 get_sku_price 为:

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

    }
  }

我想要的是将上述查询的结果(本质上是从 sku=36113 的产品中选择价格)返回到我的 View 表单上的输入变量“价格”。

我的 View 语法是:

<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 返回 200 OK 消息。可以看到post信息中包含了商品输入的内容,但是返回的html却是空白?

更新

mysql enter image description here

帖子 enter image description here

空白 HTML enter image description here

我的模型查询正确吗?该帖子是否已正确传递给模型?我如何验证这一点?

再次感谢。

最佳答案

您正在 $.post 中传递数据并将其作为 term 获取。

改变

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

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

关于codeigniter - 使用 jquery 从数据库获取单个结果的模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15542906/

相关文章:

php - 代码点火器 : Multilanguage using database and language files

javascript - 如何通过选定下拉框选项的值更改 CSS 声明

javascript - 向大型 DOM 添加类时的性能问题

jquery - jquery摇动效果后(显示:none) why?

ruby-on-rails-3 - Rake 中模型引用的未初始化常量错误

css - 在 url 末尾添加斜杠 (/) 时,codeigniter 加载 css、js、img 文件时出现奇怪行为

php - codeigniter $this->db->where();自定义字符串问题

php - 无法在 codeIgniter 上使用 session

javascript - 模型属性未统一命名的 Backbone 集合和模板

node.js - 编译 Mongoose 后无法覆盖模型