php+ajax做仿百度搜索下拉自动提示框(有实例)


Posted in PHP onAugust 21, 2012

php+mysql+ajax实现百度搜索下拉提示框 主要有3个文件三个文件在同一个目录里 如下图
php+ajax做仿百度搜索下拉自动提示框(有实例)
下面是三个文件的代码 把sql文件导入到mysql数据库里 修改下数据库密码为自己的 记得哦是UTF-8编码

php+mysql+ajax实现百度搜索下拉提示框
效果图
php+ajax做仿百度搜索下拉自动提示框(有实例)
rpc.php文件

<?php 
mysql_connect('localhost', 'root' ,''); 
mysql_select_db("test"); 
$queryString = $_POST['queryString']; 
if(strlen($queryString) >0) { 
$sql= "SELECT value FROM countries WHERE value LIKE '".$queryString."%' LIMIT 10"; 
$query = mysql_query($sql); 
while ($result = mysql_fetch_array($query,MYSQL_BOTH)){ 
$value=$result['value']; 
echo '<li onClick="fill(\''.$value.'\');">'.$value.'</li>'; 
} 
} 
?>

index.htm文件
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Ajax Auto Suggest</title> 
<script type="text/javascript" src="http://www.iiwnet.com/templets/niu/js/jquery.min.js"></script> 
<script type="text/javascript"> 
function lookup(inputString) { 
if(inputString.length == 0) { 
// Hide the suggestion box. 
$('#suggestions').hide(); 
} else { 
$.post("rpc.php", {queryString: ""+inputString+""}, function(data){ 
if(data.length >0) { 
$('#suggestions').show(); 
$('#autoSuggestionsList').html(data); 
} 
}); 
} 
} // lookup 
function fill(thisValue) { 
$('#inputString').val(thisValue); 
setTimeout("$('#suggestions').hide();", 200); 
} 
</script> 
<style type="text/css"> 
body { 
font-family: Helvetica; 
font-size: 11px; 
color: #000; 
} 
h3 { 
margin: 0px; 
padding: 0px; 
} 
.suggestionsBox { 
position: relative; 
left: 30px; 
margin: 10px 0px 0px 0px; 
width: 200px; 
background-color: #212427; 
-moz-border-radius: 7px; 
-webkit-border-radius: 7px; 
border: 2px solid #000; 
color: #fff; 
} 
.suggestionList { 
margin: 0px; 
padding: 0px; 
} 
.suggestionList li { 
margin: 0px 0px 3px 0px; 
padding: 3px; 
cursor: pointer; 
} 
.suggestionList li:hover { 
background-color: #659CD8; 
} 
</style> 
</head> 
<body> 
<div> 
<form> 
<div> 
Type your county: 
<br /> 
<input type="text" size="30" value="" id="inputString" onkeyup="lookup(this.value);" onblur="fill();" /> 
</div> 
<div class="suggestionsBox" id="suggestions" style="display: none;"> 
<img src="upArrow.png" style="position: relative; top: -12px; left: 30px;" alt="upArrow" /> 
<div class="suggestionList" id="autoSuggestionsList"> 
  
</div> 
</div> 
</form> 
</div> 
</body> 
</html>

sql数据库autoComplete.sql文件(导入到mysql)
-- phpMyAdmin SQL Dump 
-- version 3.3.5 
-- http://www.phpmyadmin.net 
-- 
-- 主机: localhost 
-- 生成日期: 2010 年 12 月 09 日 02:36 
-- 服务器版本: 5.0.22 
-- PHP 版本: 5.2.14 
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"; 
-- 
-- 数据库: `test` 
-- 
-- -------------------------------------------------------- 
-- 
-- 表的结构 `countries` 
-- 
CREATE TABLE IF NOT EXISTS `countries` ( 
`id` int(6) NOT NULL auto_increment, 
`value` varchar(250) NOT NULL default '', 
PRIMARY KEY (`id`) 
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=243 ; 
-- 
-- 转存表中的数据 `countries` 
-- 
INSERT INTO `countries` (`id`, `value`) VALUES 
(1, 'Afghanistan'), 
(2, 'Aringland Islands'), 
(3, 'Albania'), 
(4, 'Algeria'), 
(5, 'American Samoa'), 
(6, 'Andorra'), 
(7, 'Angola'), 
(8, 'Anguilla'), 
(9, 'Antarctica'), 
(10, 'Antigua and Barbuda'), 
(11, 'Argentina'), 
(12, 'Armenia'), 
(13, 'Aruba'), 
(14, 'Australia'), 
(15, 'Austria'), 
(16, 'Azerbaijan'), 
(17, 'Bahamas'), 
(18, 'Bahrain'), 
(19, 'Bangladesh'), 
(20, 'Barbados'), 
(21, 'Belarus'), 
(22, 'Belgium'), 
(23, 'Belize'), 
(24, 'Benin'), 
(25, 'Bermuda'), 
(26, 'Bhutan'), 
(27, 'Bolivia'), 
(28, 'Bosnia and Herzegovina'), 
(29, 'Botswana'), 
(30, 'Bouvet Island'), 
(31, 'Brazil'), 
(32, 'British Indian Ocean territory'), 
(33, 'Brunei Darussalam'), 
(34, 'Bulgaria'), 
(35, 'Burkina Faso'), 
(36, 'Burundi'), 
(37, 'Cambodia'), 
(38, 'Cameroon'), 
(39, 'Canada'), 
(40, 'Cape Verde'), 
(41, 'Cayman Islands'), 
(42, 'Central African Republic'), 
(43, 'Chad'), 
(44, 'Chile'), 
(45, 'China'), 
(46, 'Christmas Island'), 
(47, 'Cocos (Keeling) Islands'), 
(48, 'Colombia'), 
(49, 'Comoros'), 
(50, 'Congo'), 
(51, 'Congo'), 
(52, ' Democratic Republic'), 
(53, 'Cook Islands'), 
(54, 'Costa Rica'), 
(55, 'Ivory Coast (Ivory Coast)'), 
(56, 'Croatia (Hrvatska)'), 
(57, 'Cuba'), 
(58, 'Cyprus'), 
(59, 'Czech Republic'), 
(60, 'Denmark'), 
(61, 'Djibouti'), 
(62, 'Dominica'), 
(63, 'Dominican Republic'), 
(64, 'East Timor'), 
(65, 'Ecuador'), 
(66, 'Egypt'), 
(67, 'El Salvador'), 
(68, 'Equatorial Guinea'), 
(69, 'Eritrea'), 
(70, 'Estonia'), 
(71, 'Ethiopia'), 
(72, 'Falkland Islands'), 
(73, 'Faroe Islands'), 
(74, 'Fiji'), 
(75, 'Finland'), 
(76, 'France'), 
(77, 'French Guiana'), 
(78, 'French Polynesia'), 
(79, 'French Southern Territories'), 
(80, 'Gabon'), 
(81, 'Gambia'), 
(82, 'Georgia'), 
(83, 'Germany'), 
(84, 'Ghana'), 
(85, 'Gibraltar'), 
(86, 'Greece'), 
(87, 'Greenland'), 
(88, 'Grenada'), 
(89, 'Guadeloupe'), 
(90, 'Guam'), 
(91, 'Guatemala'), 
(92, 'Guinea'), 
(93, 'Guinea-Bissau'), 
(94, 'Guyana'), 
(95, 'Haiti'), 
(96, 'Heard and McDonald Islands'), 
(97, 'Honduras'), 
(98, 'Hong Kong'), 
(99, 'Hungary'), 
(100, 'Iceland'), 
(101, 'India'), 
(102, 'Indonesia'), 
(103, 'Iran'), 
(104, 'Iraq'), 
(105, 'Ireland'), 
(106, 'Israel'), 
(107, 'Italy'), 
(108, 'Jamaica'), 
(109, 'Japan'), 
(110, 'Jordan'), 
(111, 'Kazakhstan'), 
(112, 'Kenya'), 
(113, 'Kiribati'), 
(114, 'Korea (north)'), 
(115, 'Korea (south)'), 
(116, 'Kuwait'), 
(117, 'Kyrgyzstan'), 
(118, 'Lao People''s Democratic Republic'), 
(119, 'Latvia'), 
(120, 'Lebanon'), 
(121, 'Lesotho'), 
(122, 'Liberia'), 
(123, 'Libyan Arab Jamahiriya'), 
(124, 'Liechtenstein'), 
(125, 'Lithuania'), 
(126, 'Luxembourg'), 
(127, 'Macao'), 
(128, 'Macedonia'), 
(129, 'Madagascar'), 
(130, 'Malawi'), 
(131, 'Malaysia'), 
(132, 'Maldives'), 
(133, 'Mali'), 
(134, 'Malta'), 
(135, 'Marshall Islands'), 
(136, 'Martinique'), 
(137, 'Mauritania'), 
(138, 'Mauritius'), 
(139, 'Mayotte'), 
(140, 'Mexico'), 
(141, 'Micronesia'), 
(142, 'Moldova'), 
(143, 'Monaco'), 
(144, 'Mongolia'), 
(145, 'Montserrat'), 
(146, 'Morocco'), 
(147, 'Mozambique'), 
(148, 'Myanmar'), 
(149, 'Namibia'), 
(150, 'Nauru'), 
(151, 'Nepal'), 
(152, 'Netherlands'), 
(153, 'Netherlands Antilles'), 
(154, 'New Caledonia'), 
(155, 'New Zealand'), 
(156, 'Nicaragua'), 
(157, 'Niger'), 
(158, 'Nigeria'), 
(159, 'Niue'), 
(160, 'Norfolk Island'), 
(161, 'Northern Mariana Islands'), 
(162, 'Norway'), 
(163, 'Oman'), 
(164, 'Pakistan'), 
(165, 'Palau'), 
(166, 'Palestinian Territories'), 
(167, 'Panama'), 
(168, 'Papua New Guinea'), 
(169, 'Paraguay'), 
(170, 'Peru'), 
(171, 'Philippines'), 
(172, 'Pitcairn'), 
(173, 'Poland'), 
(174, 'Portugal'), 
(175, 'Puerto Rico'), 
(176, 'Qatar'), 
(177, 'Runion'), 
(178, 'Romania'), 
(179, 'Russian Federation'), 
(180, 'Rwanda'), 
(181, 'Saint Helena'), 
(182, 'Saint Kitts and Nevis'), 
(183, 'Saint Lucia'), 
(184, 'Saint Pierre and Miquelon'), 
(185, 'Saint Vincent and the Grenadines'), 
(186, 'Samoa'), 
(187, 'San Marino'), 
(188, 'Sao Tome and Principe'), 
(189, 'Saudi Arabia'), 
(190, 'Senegal'), 
(191, 'Serbia and Montenegro'), 
(192, 'Seychelles'), 
(193, 'Sierra Leone'), 
(194, 'Singapore'), 
(195, 'Slovakia'), 
(196, 'Slovenia'), 
(197, 'Solomon Islands'), 
(198, 'Somalia'), 
(199, 'South Africa'), 
(200, 'South Georgia and the South Sandwich Islands'), 
(201, 'Spain'), 
(202, 'Sri Lanka'), 
(203, 'Sudan'), 
(204, 'Suriname'), 
(205, 'Svalbard and Jan Mayen Islands'), 
(206, 'Swaziland'), 
(207, 'Sweden'), 
(208, 'Switzerland'), 
(209, 'Syria'), 
(210, 'Taiwan'), 
(211, 'Tajikistan'), 
(212, 'Tanzania'), 
(213, 'Thailand'), 
(214, 'Togo'), 
(215, 'Tokelau'), 
(216, 'Tonga'), 
(217, 'Trinidad and Tobago'), 
(218, 'Tunisia'), 
(219, 'Turkey'), 
(220, 'Turkmenistan'), 
(221, 'Turks and Caicos Islands'), 
(222, 'Tuvalu'), 
(223, 'Uganda'), 
(224, 'Ukraine'), 
(225, 'United Arab Emirates'), 
(226, 'United Kingdom'), 
(227, 'United States of America'), 
(228, 'Uruguay'), 
(229, 'Uzbekistan'), 
(230, 'Vanuatu'), 
(231, 'Vatican City'), 
(232, 'Venezuela'), 
(233, 'Vietnam'), 
(234, 'Virgin Islands (British)'), 
(235, 'Virgin Islands (US)'), 
(236, 'Wallis and Futuna Islands'), 
(237, 'Western Sahara'), 
(238, 'Yemen'), 
(239, 'Zaire'), 
(240, 'Zambia'), 
(241, 'Zimbabwe');

你在本地服务器测试下 一定很给力 而且原理很简单 没那么麻烦
PHP 相关文章推荐
PHP 数组实例说明
Aug 18 PHP
PHP集成FCK的函数代码
Sep 27 PHP
php一句话cmdshell新型 (非一句话木马)
Apr 18 PHP
php数组函数序列之array_unique() - 去除数组中重复的元素值
Oct 29 PHP
php图片加中文水印实现代码分享
Oct 31 PHP
用Simple Excel导出xls实现方法
Dec 06 PHP
PHP输出当前进程所有变量/常量/模块/函数/类的示例
Nov 07 PHP
PHP基于php_imagick_st-Q8.dll实现JPG合成GIF图片的方法
Jul 11 PHP
php学习笔记之基础知识
Nov 08 PHP
PHP正则表达式之捕获组与非捕获组
Nov 06 PHP
PHP流Streams、包装器wrapper概念与用法实例详解
Nov 17 PHP
PHP For循环字母A-Z当超过26个字母时输出AA,AB,AC
Feb 16 PHP
php计算十二星座的函数代码
Aug 21 #PHP
表格展示无限级分类(PHP版)
Aug 21 #PHP
gd库图片下载类实现下载网页所有图片的php代码
Aug 20 #PHP
自己在做项目过程中学到的PHP知识收集
Aug 20 #PHP
用PHP+MySQL搭建聊天室功能实例代码
Aug 20 #PHP
PHP系列学习之日期函数使用介绍
Aug 18 #PHP
PHP中extract()函数的定义和用法
Aug 17 #PHP
You might like
一首老MP3,致敬WAR3经典
2021/03/08 魔兽争霸
PHP COOKIE及时生效的方法介绍
2014/02/14 PHP
PHP面试题之文件目录操作
2015/10/15 PHP
Ext javascript建立超链接,进行事件处理的实现方法
2009/03/22 Javascript
js select常用操作控制代码
2010/03/16 Javascript
JS如何将数字类型转化为没3个一个逗号的金钱格式
2014/01/27 Javascript
jQuery窗口、文档、网页各种高度的精确理解
2014/07/02 Javascript
JS 作用域与作用域链详解
2015/04/07 Javascript
深入理解JavaScript中的对象
2015/06/04 Javascript
a标签跳转到指定div,jquery添加和移除class属性的实现方法
2016/10/10 Javascript
详解Javascript中的原型OOP
2016/10/12 Javascript
Vue.js组件tree实现省市多级联动
2016/12/02 Javascript
js实现悬浮窗效果(支持拖动)
2017/03/09 Javascript
详解Node全局变量global模块
2017/09/28 Javascript
Angular实现表单验证功能
2017/11/13 Javascript
JS/jQuery实现简单的开关灯效果【案例】
2019/02/19 jQuery
使用vue重构资讯页面的实例代码解析
2019/11/26 Javascript
echarts柱状图背景重叠组合而非并列的实现代码
2020/12/10 Javascript
[02:02:38]VG vs Mineski Supermajor 败者组 BO3 第一场 6.6
2018/06/07 DOTA
python cookielib 登录人人网的实现代码
2012/12/19 Python
python中单下划线_的常见用法总结
2018/07/10 Python
Python实现的爬取百度文库功能示例
2019/02/16 Python
Python搭建代理IP池实现检测IP的方法
2019/10/27 Python
Python用input输入列表的实例代码
2020/02/07 Python
python列表返回重复数据的下标
2020/02/10 Python
python爬取youtube视频的示例代码
2021/03/03 Python
英国领先品牌手动工具和电动工具供应商:Tooled Up
2018/11/24 全球购物
期末考试动员演讲稿
2014/01/10 职场文书
农村党支部先进事迹
2014/01/14 职场文书
自我推荐信范文
2014/05/09 职场文书
阅兵口号
2014/06/19 职场文书
幼儿园教师节演讲稿
2014/09/03 职场文书
离职报告格式
2014/11/04 职场文书
丽江古城导游词
2015/02/03 职场文书
Python中itertools库的四个函数介绍
2022/04/06 Python
Linux下使用C语言代码搭建一个简单的HTTP服务器
2022/04/13 Servers