欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-11-12 20:42:28
php+mysql+ajax实现百度搜索下拉提示框 主要有3个文件三个文件在同一个目录里 如下图 下面是三个文件的代码 把sql文件导入到mysql数据库里 修改下数据库密...
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, '*'),
(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, '*'),
(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, '*'),
(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');

你在本地服务器测试下 一定很给力 而且原理很简单 没那么麻烦