自动完成功能

先放个图片吧!这是最终的效果!

下面具体代码

index.php

    <scrīpt type="text/javascrīpt" src="javascrīpts/prototype.js"></scrīpt>
    <scrīpt type="text/javascrīpt" src="javascrīpts/autocomplete.js"></scrīpt>

把需要的JS代码库包含进来

<h3>请输入搜索关键字</h3>
<table>
<tr>
<td>Keywords Here</td>
<td><input type="text" name="keyword" id="keyword" size="54" ōnchange="pSearch()" /></td>
<td><input type="submit" name="submit" value="search"></td>
</tr>
</table>
<scrīpt type="text/javascrīpt">
  
    new CAPXOUS.AutoComplete("keyword", function() {
        return "auto.php?keyword=" + this.text.value;
    });

</scrīpt>

这里是具体调用的代码

auto.php


<?php
 header('Content-Type:text/html;charset=GB2312');

 header("Cache-Control: no-cache, must-revalidate");
 include('conn.inc.php');
 $conn = getConn();
 $keyword = trim($_GET['keyword']);
 $sql = "select * from channel where keywords like '".$keyword."%' limit 0,10";
 $result = query($sql,$conn);
 if(nums($result)){
?>
<?php
  while ($row = fetch($result)){
?>
  <div ōnselect="this.text.value = '<?=iconv("GBK","UTF-8",$row['sokey'])?>';">
            <b><?=iconv("GBK","UTF-8",$row['sokey'])?></b></div>
<?php   
  }
 }
 ?>

从数据库取出的数据需要转换一下编码!不然中文会出现乱码!

具体的演示可以登录
http://so.jobmet.com
posted @ 2008-12-19 14:31  Athrun  阅读(294)  评论(0编辑  收藏  举报