jquery 自动完成远程源尝试了一切!但没有好处

发布于 2024-12-15 10:02:30 字数 1863 浏览 2 评论 0原文

我一直在尝试实现 jquery ui 自动完成功能..并且基本的 html 代码与给定的完全相同 - http://jqueryui.com/demos/autocomplete/#remote

我已经尝试了所有组合/ “search.php”的代码片段,例如 -

1)

<?php 

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

// connect to the database server and select the appropriate database for use
$dblink = mysql_connect('localhost', 'root', '') or die( mysql_error() );
mysql_select_db('research');

$rs = mysql_query('select uname,email,password from login where uname like "'. mysql_real_escape_string($_REQUEST['term']) .'%" order by uname asc limit 0,10', $dblink);

// loop through each zipcode returned and format the response for jQuery
$data = array();
if ( $rs && mysql_num_rows($rs) )
{
    while( $row = mysql_fetch_array($rs, MYSQL_ASSOC) )
    {
        $data[] = array(
            'label' => $row['uname'] .', '. $row['password'] .' '.$row['uname'] ,
            'value' => $row['uname']
        );
    }
}

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

2)

<?php 
include("includes/connect.php");

$query = "SELECT uname from login WHERE uname LIKE '%" . addslashes($_GET['term']) . "%'";

$result = mysql_query($query);

while($row = mysql_fetch_assoc($result)) {

foreach($row as $val)
    $tab[] = $val;

}

print json_encode($tab);
?>

3)

<?php

include("includes/connect.php");

$term = $_REQUEST['m']; // where name of the text field is 'm'
$query = "SELECT * FROM login WHERE uname LIKE '%$term%'";
$result = $mysqli->query($query);
$arr = array();
while($obj = $result->fetch_assoc()) {
      $arr[] = $obj['nome'];
}

echo json_encode($arr);

?>

但仍然没有任何效果!也就是说,当我在自动完成文本框中键入内容时,没有显示任何结果。可能是什么错误?有没有其他解决方案?请帮忙!

i have been trying to implement jquery ui autocomplete feature.. and the basic html code is xactly the same as given on-
http://jqueryui.com/demos/autocomplete/#remote

I have tried all combinations/ code snippets for "search.php" such as-

1)

<?php 

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

// connect to the database server and select the appropriate database for use
$dblink = mysql_connect('localhost', 'root', '') or die( mysql_error() );
mysql_select_db('research');

$rs = mysql_query('select uname,email,password from login where uname like "'. mysql_real_escape_string($_REQUEST['term']) .'%" order by uname asc limit 0,10', $dblink);

// loop through each zipcode returned and format the response for jQuery
$data = array();
if ( $rs && mysql_num_rows($rs) )
{
    while( $row = mysql_fetch_array($rs, MYSQL_ASSOC) )
    {
        $data[] = array(
            'label' => $row['uname'] .', '. $row['password'] .' '.$row['uname'] ,
            'value' => $row['uname']
        );
    }
}

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

2)

<?php 
include("includes/connect.php");

$query = "SELECT uname from login WHERE uname LIKE '%" . addslashes($_GET['term']) . "%'";

$result = mysql_query($query);

while($row = mysql_fetch_assoc($result)) {

foreach($row as $val)
    $tab[] = $val;

}

print json_encode($tab);
?>

3)

<?php

include("includes/connect.php");

$term = $_REQUEST['m']; // where name of the text field is 'm'
$query = "SELECT * FROM login WHERE uname LIKE '%$term%'";
$result = $mysqli->query($query);
$arr = array();
while($obj = $result->fetch_assoc()) {
      $arr[] = $obj['nome'];
}

echo json_encode($arr);

?>

But still nothing is working !! that is when i type into the text box for autocomplete there are no results shown. what could be the error ? are there any alternative solutions ?? Please help !!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉酒的小男人 2024-12-22 10:02:30

传递到 json_encode 的变量必须是字符串数组。
例如[“红”,“绿”,“蓝”]
或对象文字数组,例如 [{"value" : "red"},{"value" : "green"},{"value" : "blue"}]

每次循环时,您都需要将项目附加到现有的项目。

我注意到,在所有 3 个代码片段中,您将当前项分配给数组变量,而不是附加它。循环完成后,数组中的唯一项目将是循环最近处理的项目。

因此,除非您在字段中输入的内容与该一项匹配,否则自动完成功能不会显示任何内容。

下面是我的 PHP 代码,用于在将字符串数组发送回发出请求的文件之前构建该数组。

    $fetchedArtists = $db->query($queryToGetArtists );

    $json = '[';
    $first = true;

    while($row = $fetchedArtists->fetch_assoc())
    {
        if (!$first) 
        {
            $json .=  ',';
        } 
        else 
        {
            $first = false;
        }

        $artist = $row['artistName'];     
        $json .= '{"value":"'.$artist.'"}';
    }
    $json .= ']';
    echo $json;

The variable that you pass into json_encode must be an array of strings.
e.g. ["red","green","blue"]
or an array of object literals e.g. [{"value" : "red"},{"value" : "green"},{"value" : "blue"}]

Each time through the loop you need to append the item to the existing items.

I notice, in all 3 code snippets, that you assign the current item to your array variable, instead of appending it. When the loop completes, the only item in your array will be the item processed most recently by the loop.

So unless what you type in the field matches that one item, the autocomplete isn't going to show anything.

Here is my PHP code to build an array of strings before sending it back to the file that made the request.

    $fetchedArtists = $db->query($queryToGetArtists );

    $json = '[';
    $first = true;

    while($row = $fetchedArtists->fetch_assoc())
    {
        if (!$first) 
        {
            $json .=  ',';
        } 
        else 
        {
            $first = false;
        }

        $artist = $row['artistName'];     
        $json .= '{"value":"'.$artist.'"}';
    }
    $json .= ']';
    echo $json;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文