2016-02-12 79 views
0

我已经为我的网络实现了jQuery自动完成功能,它工作正常。但是我希望自动完成的结果仅检索特定人员的数据,而不是完整的数据库结果。jQuery自动完成从数据库中检索数据

下面是jQuery的用于自动完成

jQuery(document).ready(function($){ 
$('.product_desc').autocomplete({source:'functions/products.php?', minLength:2}); 

products.php

//Path for the databsae 
<?php 
include '../include/configuration.php'; 

if (!isset($_REQUEST['term'])) 
    exit; 

$rs = mysql_query('select id,item_name,fk_vendor_id from item where item_name like "%'. mysql_real_escape_string($_REQUEST['term']).'%" order by item_name asc ', $con); 

$data = array(); 

if ($rs && mysql_num_rows($rs)) 
{ 
    while($row = mysql_fetch_array($rs, MYSQL_ASSOC)) 
    { 
     $data[] = array(
      'label' => $row['item_name'], 
      'value' => $row['item_name'] 
     ); 
    } 
} 
else 
{ 
    $data[] = array(
     'label' => 'not found', 
     'value' =>'' 
    ); 
} 

// jQuery wants JSON data 
echo json_encode($data); 
flush(); 
?> 

任何解决方案将受到赞赏。

+0

如果您想要一条记录,请在查询中设置LIMIT 1或在查询中传递用户ID。 –

+0

嗨Ravi,我试着传递用户ID,但它没有奏效。 –

+0

你可以发布该查询吗? –

回答

0

试试这个:

$(".product_desc").autocomplete({ 
source: "functions/products.php", 
minLength: 2, 
select: function(event,ui){ 
    //do something 
    } 
}); 
0

试试这个代码,任何文本字段上课.search自动完成建议你将需要在ajax.php文件服务器端的作品返回数组如下:

$response = ['Computer', 'Mouse', 'Keyboard', 'Monitor']; 
echo json_encode($response); 

这里是自动建议的示例代码。

$(document).on('keyups','.search',function() { 
     $(this).autocomplete({ 
      source: function(request, response) { 
       if (request.term != "") { 
        $.ajax({ 
         url: "ajax.php", 
         dataType: "json", 
         method: "post", 
         data: { 
          name: request.term 
         }, 
         success: function (data) { 
          if (data != "") { 
           response($.map(data, function (item) { 
            var name = item.name; 
            return { 
             label: name, 
             value: name, 
             data: item 
            } 
           })); 
          } 
         } 
        }); 
       } 
      }, 
      autoFocus: true, 
      minLength: 2, 
      select: function(event, ui) { 
       var name = ui.item.data; 
       $(this).val(name); 
      } 
     }); 
    });