jQuery UI自动完成搜索结果不显示 [英] JQuery UI Autocomplete Search Results do not display

查看:108
本文介绍了jQuery UI自动完成搜索结果不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

array我无法使用php从mysql数据库中获取搜索结果.

arrayI am getting no search results from my mysql database using php.

PHP代码:

require_once "connectmysql.php";
$belongsto=$current_user->businessname;
$q = trim(strip_tags($_GET["term"]));
if (!$q) return;
$sql = "select clientname as value from zb_clients where clientname LIKE '%".$q."%' AND belongsto='".$belongsto."'";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
    $row['value']=htmlentities(stripslashes($row['value']));
    $row_set[] = $row;
}
echo json_encode($row_set);

jQuery代码:

<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.1/themes/base/minified/jquery-ui.min.css" type="text/css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.10.1/jquery-ui.min.js"></script>

<script type="text/javascript">
$(function() {

    //autocomplete
    $("#search").autocomplete({
        source: "../searchclient.php",
        minLength: 1,
    });             

});
</script>

输入字段:

<input type="text" name="search" id="search" placeholder="Search for Business Name" />

我相信php代码是正确的.如果我自己运行php代码并使用 /searchclient.php?term=a

I believe the php code is correct. If I run the php code on its own and use /searchclient.php?term=a

作为示例,它在数组中返回我想要的结果.

as an example, it returns the results that I want in an array.

例如[{"value":"Hello World"},{"value":"East Meets West JV"}].

如果我替换了Jquery行

If I replace the Jquery line

source: "../searchclient.php",

使用

source: [ "c++", "java", "php", "coldfusion", "javascript", "asp", "ruby" ],

然后,自动完成功能将与该来源一起使用.因此,将数组传递回JQuery肯定存在问题.

then the automocomplete works with that source. So there must be an issue with the array passing back into JQuery.

我不能完全把手指放在上面.我是否缺少一些重要的东西?

I cant quite put my finger on it. Am I missing something crucial?

我曾尝试使用Firebug进行调试,但未返回任何错误.

Ive tried debugging with firebug but its not returning any errors.

任何帮助将不胜感激!

编辑的PHP代码:

require_once "connectmysql.php";
$belongsto=$current_user->businessname;
$q = $_GET["term"];
if (!$q) return;
$sql = "select clientname as value, idzb_clients as id from zb_clients where clientname LIKE '%".$q."%' AND belongsto='".$belongsto."'";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
    $row['id']=htmlentities(stripslashes($row['id']));
    $row['value']=htmlentities(stripslashes($row['value']));
    $row['label']=htmlentities(stripslashes($row['value']));
    $row_set[] = $row;
}
echo json_encode($row_set);

推荐答案

我沿着AJAX路线行驶,这似乎行得通:

I went down the AJAX route and this seemed to work:

HTML

$( "#search" ).autocomplete({
  source: function( request, response ) {
    $.ajax({
      url: "../searchclient.php",
      dataType: "json",
      data: {
        q: request.term
      },
      success: function( data ) {
        response( data );
      }
    });
  },
  minLength: 1
});

SEARCHCLIENT.PHP

<?php

require_once "connectmysql.php";
$belongsto = $current_user->businessname;
$q = $_GET['q'];
$sql = "select clientname as value, idzb_clients as id, has_ledger_setup from zb_clients where clientname LIKE '%".$q."%' AND belongsto='".$belongsto."'";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
    $row['id'] = htmlentities(stripslashes($row['id']));
    $row['value'] = htmlentities(stripslashes($row['value']));
    $row['label'] = htmlentities(stripslashes($row['value']));
    $row_set[] = $row;
}
header('Content-Type: application/json');
echo json_encode($row_set);

?>

这篇关于jQuery UI自动完成搜索结果不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆