- 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
- 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧
> 后端 > php > php+ajax做仿百度搜索下拉自动提示框(有实例)_php实例 php+mysql+ajax实现百度搜索下拉提示框 主要有3个文件三个文件在同一个目录里 如下图
下面是三个文件的代码 把sql文件导入到mysql数据库里 修改下数据库密码为自己的 记得哦是UTF-8编码
php+mysql+ajax实现百度搜索下拉提示框
效果图
rpc.php文件
<BR><?php <BR>mysql_connect('localhost', 'root' ,''); <BR>mysql_select_db("test"); <BR>$queryString = $_POST['queryString']; <BR>if(strlen($queryString) >0) { <BR>$sql= "SELECT value FROM countries WHERE value LIKE '".$queryString."%' LIMIT 10"; <BR>$query = mysql_query($sql); <BR>while ($result = mysql_fetch_array($query,MYSQL_BOTH)){ <BR>$value=$result['value']; <BR>echo '<li>'.$value.'</li>'; <BR>} <BR>} <BR>?> <BR>
index.htm文件
<BR> <BR> <BR> <BR><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <BR><title>Ajax Auto Suggest</title> <BR><script type="text/javascript" src="http://www.iiwnet.com/templets/niu/js/jquery.min.js"></script> <BR><script type="text/javascript"> <BR>function lookup(inputString) { <BR>if(inputString.length == 0) { <BR>// Hide the suggestion box. <BR>$('#suggestions').hide(); <BR>} else { <BR>$.post("rpc.php", {queryString: ""+inputString+""}, function(data){ <BR>if(data.length >0) { <BR>$('#suggestions').show(); <BR>$('#autoSuggestionsList').html(data); <BR>} <BR>}); <BR>} <BR>} // lookup <BR>function fill(thisValue) { <BR>$('#inputString').val(thisValue); <BR>setTimeout("$('#suggestions').hide();", 200); <BR>} <BR></script> <BR><style type="text/css"> <BR>body { <BR>font-family: Helvetica; <BR>font-size: 11px; <BR>color: #000; <BR>} <BR>h3 { <BR>margin: 0px; <BR>padding: 0px; <BR>} <BR>.suggestionsBox { <BR>position: relative; <BR>left: 30px; <BR>margin: 10px 0px 0px 0px; <BR>width: 200px; <BR>background-color: #212427; <BR>-moz-border-radius: 7px; <BR>-webkit-border-radius: 7px; <BR>border: 2px solid #000; <BR>color: #fff; <BR>} <BR>.suggestionList { <BR>margin: 0px; <BR>padding: 0px; <BR>} <BR>.suggestionList li { <BR>margin: 0px 0px 3px 0px; <BR>padding: 3px; <BR>cursor: pointer; <BR>} <BR>.suggestionList li:hover { <BR>background-color: #659CD8; <BR>} <BR></style> <BR> <BR><body> <BR><div> <BR> <BR><div> <BR>Type your county: <BR><br /> <BR> <BR>
sql数据库autoComplete.sql文件(导入到mysql)
<BR>-- phpMyAdmin SQL Dump <BR>-- version 3.3.5 <BR>-- http://www.phpmyadmin.net <BR>-- <BR>-- 主机: localhost <BR>-- 生成日期: 2010 年 12 月 09 日 02:36 <BR>-- 服务器版本: 5.0.22 <BR>-- PHP 版本: 5.2.14 <BR>SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"; <BR>-- <BR>-- 数据库: `test` <BR>-- <BR>-- -------------------------------------------------------- <BR>-- <BR>-- 表的结构 `countries` <BR>-- <BR>CREATE TABLE IF NOT EXISTS `countries` ( <BR>`id` int(6) NOT NULL auto_increment, <BR>`value` varchar(250) NOT NULL default '', <BR>PRIMARY KEY (`id`) <BR>) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=243 ; <BR>-- <BR>-- 转存表中的数据 `countries` <BR>-- <BR>INSERT INTO `countries` (`id`, `value`) VALUES <BR>(1, 'Afghanistan'), <BR>(2, 'Aringland Islands'), <BR>(3, 'Albania'), <BR>(4, 'Algeria'), <BR>(5, 'American Samoa'), <BR>(6, 'Andorra'), <BR>(7, 'Angola'), <BR>(8, 'Anguilla'), <BR>(9, 'Antarctica'), <BR>(10, 'Antigua and Barbuda'), <BR>(11, 'Argentina'), <BR>(12, 'Armenia'), <BR>(13, 'Aruba'), <BR>(14, 'Australia'), <BR>(15, 'Austria'), <BR>(16, 'Azerbaijan'), <BR>(17, 'Bahamas'), <BR>(18, 'Bahrain'), <BR>(19, 'Bangladesh'), <BR>(20, 'Barbados'), <BR>(21, 'Belarus'), <BR>(22, 'Belgium'), <BR>(23, 'Belize'), <BR>(24, 'Benin'), <BR>(25, 'Bermuda'), <BR>(26, 'Bhutan'), <BR>(27, 'Bolivia'), <BR>(28, 'Bosnia and Herzegovina'), <BR>(29, 'Botswana'), <BR>(30, 'Bouvet Island'), <BR>(31, 'Brazil'), <BR>(32, 'British Indian Ocean territory'), <BR>(33, 'Brunei Darussalam'), <BR>(34, 'Bulgaria'), <BR>(35, 'Burkina Faso'), <BR>(36, 'Burundi'), <BR>(37, 'Cambodia'), <BR>(38, 'Cameroon'), <BR>(39, 'Canada'), <BR>(40, 'Cape Verde'), <BR>(41, 'Cayman Islands'), <BR>(42, 'Central African Republic'), <BR>(43, 'Chad'), <BR>(44, 'Chile'), <BR>(45, 'China'), <BR>(46, 'Christmas Island'), <BR>(47, 'Cocos (Keeling) Islands'), <BR>(48, 'Colombia'), <BR>(49, 'Comoros'), <BR>(50, 'Congo'), <BR>(51, 'Congo'), <BR>(52, ' Democratic Republic'), <BR>(53, 'Cook Islands'), <BR>(54, 'Costa Rica'), <BR>(55, 'Ivory Coast (Ivory Coast)'), <BR>(56, 'Croatia (Hrvatska)'), <BR>(57, 'Cuba'), <BR>(58, 'Cyprus'), <BR>(59, 'Czech Republic'), <BR>(60, 'Denmark'), <BR>(61, 'Djibouti'), <BR>(62, 'Dominica'), <BR>(63, 'Dominican Republic'), <BR>(64, 'East Timor'), <BR>(65, 'Ecuador'), <BR>(66, 'Egypt'), <BR>(67, 'El Salvador'), <BR>(68, 'Equatorial Guinea'), <BR>(69, 'Eritrea'), <BR>(70, 'Estonia'), <BR>(71, 'Ethiopia'), <BR>(72, 'Falkland Islands'), <BR>(73, 'Faroe Islands'), <BR>(74, 'Fiji'), <BR>(75, 'Finland'), <BR>(76, 'France'), <BR>(77, 'French Guiana'), <BR>(78, 'French Polynesia'), <BR>(79, 'French Southern Territories'), <BR>(80, 'Gabon'), <BR>(81, 'Gambia'), <BR>(82, 'Georgia'), <BR>(83, 'Germany'), <BR>(84, 'Ghana'), <BR>(85, 'Gibraltar'), <BR>(86, 'Greece'), <BR>(87, 'Greenland'), <BR>(88, 'Grenada'), <BR>(89, 'Guadeloupe'), <BR>(90, 'Guam'), <BR>(91, 'Guatemala'), <BR>(92, 'Guinea'), <BR>(93, 'Guinea-Bissau'), <BR>(94, 'Guyana'), <BR>(95, 'Haiti'), <BR>(96, 'Heard and McDonald Islands'), <BR>(97, 'Honduras'), <BR>(98, 'Hong Kong'), <BR>(99, 'Hungary'), <BR>(100, 'Iceland'), <BR>(101, 'India'), <BR>(102, 'Indonesia'), <BR>(103, 'Iran'), <BR>(104, 'Iraq'), <BR>(105, 'Ireland'), <BR>(106, 'Israel'), <BR>(107, 'Italy'), <BR>(108, 'Jamaica'), <BR>(109, 'Japan'), <BR>(110, 'Jordan'), <BR>(111, 'Kazakhstan'), <BR>(112, 'Kenya'), <BR>(113, 'Kiribati'), <BR>(114<span style="color:transparent">~来1源gaodai#ma#com搞*代#码1网</span><abbr>搞代gaodaima码</abbr>, 'Korea (north)'), <BR>(115, 'Korea (south)'), <BR>(116, 'Kuwait'), <BR>(117, 'Kyrgyzstan'), <BR>(118, 'Lao People''s Democratic Republic'), <BR>(119, 'Latvia'), <BR>(120, 'Lebanon'), <BR>(121, 'Lesotho'), <BR>(122, 'Liberia'), <BR>(123, 'Libyan Arab Jamahiriya'), <BR>(124, 'Liechtenstein'), <BR>(125, 'Lithuania'), <BR>(126, 'Luxembourg'), <BR>(127, 'Macao'), <BR>(128, 'Macedonia'), <BR>(129, 'Madagascar'), <BR>(130, 'Malawi'), <BR>(131, 'Malaysia'), <BR>(132, 'Maldives'), <BR>(133, 'Mali'), <BR>(134, 'Malta'), <BR>(135, 'Marshall Islands'), <BR>(136, 'Martinique'), <BR>(137, 'Mauritania'), <BR>(138, 'Mauritius'), <BR>(139, 'Mayotte'), <BR>(140, 'Mexico'), <BR>(141, 'Micronesia'), <BR>(142, 'Moldova'), <BR>(143, 'Monaco'), <BR>(144, 'Mongolia'), <BR>(145, 'Montserrat'), <BR>(146, 'Morocco'), <BR>(147, 'Mozambique'), <BR>(148, 'Myanmar'), <BR>(149, 'Namibia'), <BR>(150, 'Nauru'), <BR>(151, 'Nepal'), <BR>(152, 'Netherlands'), <BR>(153, 'Netherlands Antilles'), <BR>(154, 'New Caledonia'), <BR>(155, 'New Zealand'), <BR>(156, 'Nicaragua'), <BR>(157, 'Niger'), <BR>(158, 'Nigeria'), <BR>(159, 'Niue'), <BR>(160, 'Norfolk Island'), <BR>(161, 'Northern Mariana Islands'), <BR>(162, 'Norway'), <BR>(163, 'Oman'), <BR>(164, 'Pakistan'), <BR>(165, 'Palau'), <BR>(166, 'Palestinian Territories'), <BR>(167, 'Panama'), <BR>(168, 'Papua New Guinea'), <BR>(169, 'Paraguay'), <BR>(170, 'Peru'), <BR>(171, 'Philippines'), <BR>(172, 'Pitcairn'), <BR>(173, 'Poland'), <BR>(174, 'Portugal'), <BR>(175, 'Puerto Rico'), <BR>(176, 'Qatar'), <BR>(177, 'Runion'), <BR>(178, 'Romania'), <BR>(179, 'Russian Federation'), <BR>(180, 'Rwanda'), <BR>(181, 'Saint Helena'), <BR>(182, 'Saint Kitts and Nevis'), <BR>(183, 'Saint Lucia'), <BR>(184, 'Saint Pierre and Miquelon'), <BR>(185, 'Saint Vincent and the Grenadines'), <BR>(186, 'Samoa'), <BR>(187, 'San Marino'), <BR>(188, 'Sao Tome and Principe'), <BR>(189, 'Saudi Arabia'), <BR>(190, 'Senegal'), <BR>(191, 'Serbia and Montenegro'), <BR>(192, 'Seychelles'), <BR>(193, 'Sierra Leone'), <BR>(194, 'Singapore'), <BR>(195, 'Slovakia'), <BR>(196, 'Slovenia'), <BR>(197, 'Solomon Islands'), <BR>(198, 'Somalia'), <BR>(199, 'South Africa'), <BR>(200, 'South Georgia and the South Sandwich Islands'), <BR>(201, 'Spain'), <BR>(202, 'Sri Lanka'), <BR>(203, 'Sudan'), <BR>(204, 'Suriname'), <BR>(205, 'Svalbard and Jan Mayen Islands'), <BR>(206, 'Swaziland'), <BR>(207, 'Sweden'), <BR>(208, 'Switzerland'), <BR>(209, 'Syria'), <BR>(210, 'Taiwan'), <BR>(211, 'Tajikistan'), <BR>(212, 'Tanzania'), <BR>(213, 'Thailand'), <BR>(214, 'Togo'), <BR>(215, 'Tokelau'), <BR>(216, 'Tonga'), <BR>(217, 'Trinidad and Tobago'), <BR>(218, 'Tunisia'), <BR>(219, 'Turkey'), <BR>(220, 'Turkmenistan'), <BR>(221, 'Turks and Caicos Islands'), <BR>(222, 'Tuvalu'), <BR>(223, 'Uganda'), <BR>(224, 'Ukraine'), <BR>(225, 'United Arab Emirates'), <BR>(226, 'United Kingdom'), <BR>(227, 'United States of America'), <BR>(228, 'Uruguay'), <BR>(229, 'Uzbekistan'), <BR>(230, 'Vanuatu'), <BR>(231, 'Vatican City'), <BR>(232, 'Venezuela'), <BR>(233, 'Vietnam'), <BR>(234, 'Virgin Islands (British)'), <BR>(235, 'Virgin Islands (US)'), <BR>(236, 'Wallis and Futuna Islands'), <BR>(237, 'Western Sahara'), <BR>(238, 'Yemen'), <BR>(239, 'Zaire'), <BR>(240, 'Zambia'), <BR>(241, 'Zimbabwe'); <BR>
你在本地服务器测试下 一定很给力 而且原理很简单 没那么麻烦