【问题标题】:jQuery autocomplete retrieve data from databasejQuery自动完成从数据库中检索数据
【发布时间】:2016-05-23 10:45:01
【问题描述】:

我已经为我的网站实现了 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();
?>

任何解决方案将不胜感激。

【问题讨论】:

  • 如果您想要一条记录,请在查询中设置 LIMIT 1 或在查询中传递用户 ID。
  • 嗨 Ravi,我尝试传递用户 ID,但没有成功。
  • 您也可以发布该查询吗?
  • @RaviHirani 我在上面发布的 products.php 中使用了 $_SESSION[user_id] 代码。但是当我使用该全局变量时,查询不起作用。
  • 我看还没有答案被接受,所以如果你有兴趣,我可以替代 jquery 的自动完成功能来从数据库中获取结果:rufushale.com/article.php?w=19

标签: javascript php jquery mysql autocomplete


【解决方案1】:

试试这个代码,任何具有 .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);
            }
        });
    });

【讨论】:

    【解决方案2】:

    试试这个:

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

    【讨论】:

      猜你喜欢
      • 2013-04-26
      • 1970-01-01
      • 1970-01-01
      • 2021-09-26
      • 1970-01-01
      • 2011-02-21
      • 2016-03-04
      • 2012-04-09
      • 1970-01-01
      相关资源
      最近更新 更多