Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7334046
  • 博文数量: 5645
  • 博客积分: 9880
  • 博客等级: 中将
  • 技术积分: 68080
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-28 13:35
文章分类

全部博文(5645)

文章存档

2008年(5645)

我的朋友

分类:

2008-04-28 14:20:12

下载本文示例代码

我觉得我有必要写这个教程,因为曾经见到的大部分关于自动完成的应用程序都只是给你一个程序源码包,然后告诉你怎么使用,而不是告诉你它是如何工作的以及为什么这样做。而知道这些可以让你对这个插件可以进一步的按自己的需求定制(关于这一点我在我的blog里写过不少关于其他应用的文章)。


好,我们现在开始。

JavaScript代码:


JS的解释:

好,从上面的代码看到,我们需要连接到一个叫做rpc.php的文件,这个文件处理所有的操作。

lookup函数使用从文本输入框中得到的单词然后使用jQuery中Ajax的方法POST把它传给rpc.php。

如果输入字符 ‘inputString’是‘0’(Zero,译注:在这里是指在搜索框中没输入任何内容),建议框就被隐藏,这也很人性化,你想,如果在搜索框中没有输入任何东西,你也不期望会出现个建议提示框。

如果输入框中有内容,我们就得到了这个 ‘inputString’并传递给rpc.php页面,然后jQuery 的$.post()函数被使用,如下:

$.post(url, [data], [callback])
‘callback’部分可以关联一个函数,这个比较有意思,只有在数据(data)被加载成功的时候才会执行(译注:此处为意译,没看懂原文:<).

如果返回的数据(data)不为空(也就是说,有东西要显示),那就显示搜索提示框并且使用返回的数据(data)来代替其中的html代码。

就这么简单!

PHP后台程序(rpc.php):

如你所知(译注:不好意思,看王小波就学会了这么个口头禅),我的php后台程序都叫做rpc.php(RPC指远程过程调用),而没用它实际执行的功能来命名,但是也还不错了。

// PHP5 Implementation - uses MySQLi.
$db = new mysqli(‘localhost’, ‘root’ ,”, ‘autoComplete’);
if(!$db) {
// Show error if we cannot connect.
echo ‘ERROR: Could not connect to the database.’;
} else {
// Is there a posted query string?
if(isset($_POST[‘queryString’])) {
$queryString = $_POST[‘queryString’];
// Is the string length greater than 0?
if(strlen($queryString) >0) {
// Run the query: We use LIKE ‘$queryString%’
// The percentage sign is a wild-card, in my example of countries it works like this…
// $queryString = ‘Uni’;
// Returned data = ‘United States, United Kindom’;
$query = $db->query("SELECT value FROM countries WHERE value LIKE ‘$queryString%’ LIMIT 10");
if($query) {
// While there are results loop through them - fetching an Object (i like PHP5 btw!).
while ($result = $query ->fetch_object()) {
// Format the results, im using

  • for the list, you can change it.
    // The onClick function fills the textbox with the result.
    echo ‘
  • ’.$result->value.‘
  • ’;
    }
    } else {
    echo ‘ERROR: There was a problem with the query.’;
    }
    } else {
    // Dont do anything.
    } // There is a queryString.
    } else {
    echo ‘There should be no direct access to this script!’;
    }
    }

    ?>

    我觉得我有必要写这个教程,因为曾经见到的大部分关于自动完成的应用程序都只是给你一个程序源码包,然后告诉你怎么使用,而不是告诉你它是如何工作的以及为什么这样做。而知道这些可以让你对这个插件可以进一步的按自己的需求定制(关于这一点我在我的blog里写过不少关于其他应用的文章)。


    好,我们现在开始。

    JavaScript代码:


    JS的解释:

    好,从上面的代码看到,我们需要连接到一个叫做rpc.php的文件,这个文件处理所有的操作。

    lookup函数使用从文本输入框中得到的单词然后使用jQuery中Ajax的方法POST把它传给rpc.php。

    如果输入字符 ‘inputString’是‘0’(Zero,译注:在这里是指在搜索框中没输入任何内容),建议框就被隐藏,这也很人性化,你想,如果在搜索框中没有输入任何东西,你也不期望会出现个建议提示框。

    如果输入框中有内容,我们就得到了这个 ‘inputString’并传递给rpc.php页面,然后jQuery 的$.post()函数被使用,如下:

    $.post(url, [data], [callback])
    ‘callback’部分可以关联一个函数,这个比较有意思,只有在数据(data)被加载成功的时候才会执行(译注:此处为意译,没看懂原文:<).

    如果返回的数据(data)不为空(也就是说,有东西要显示),那就显示搜索提示框并且使用返回的数据(data)来代替其中的html代码。

    就这么简单!

    PHP后台程序(rpc.php):

    如你所知(译注:不好意思,看王小波就学会了这么个口头禅),我的php后台程序都叫做rpc.php(RPC指远程过程调用),而没用它实际执行的功能来命名,但是也还不错了。

    // PHP5 Implementation - uses MySQLi.
    $db = new mysqli(‘localhost’, ‘root’ ,”, ‘autoComplete’);
    if(!$db) {
    // Show error if we cannot connect.
    echo ‘ERROR: Could not connect to the database.’;
    } else {
    // Is there a posted query string?
    if(isset($_POST[‘queryString’])) {
    $queryString = $_POST[‘queryString’];
    // Is the string length greater than 0?
    if(strlen($queryString) >0) {
    // Run the query: We use LIKE ‘$queryString%’
    // The percentage sign is a wild-card, in my example of countries it works like this…
    // $queryString = ‘Uni’;
    // Returned data = ‘United States, United Kindom’;
    $query = $db->query("SELECT value FROM countries WHERE value LIKE ‘$queryString%’ LIMIT 10");
    if($query) {
    // While there are results loop through them - fetching an Object (i like PHP5 btw!).
    while ($result = $query ->fetch_object()) {
    // Format the results, im using

  • for the list, you can change it.
    // The onClick function fills the textbox with the result.
    echo ‘
  • ’.$result->value.‘
  • ’;
    }
    } else {
    echo ‘ERROR: There was a problem with the query.’;
    }
    } else {
    // Dont do anything.
    } // There is a queryString.
    } else {
    echo ‘There should be no direct access to this script!’;
    }
    }

    ?>

    下载本文示例代码


    搜索自动提示的PHP JS方法搜索自动提示的PHP JS方法搜索自动提示的PHP JS方法搜索自动提示的PHP JS方法搜索自动提示的PHP JS方法搜索自动提示的PHP JS方法搜索自动提示的PHP JS方法搜索自动提示的PHP JS方法搜索自动提示的PHP JS方法搜索自动提示的PHP JS方法搜索自动提示的PHP JS方法搜索自动提示的PHP JS方法搜索自动提示的PHP JS方法搜索自动提示的PHP JS方法搜索自动提示的PHP JS方法
    阅读(238) | 评论(0) | 转发(0) |
    给主人留下些什么吧!~~