Php + ajax do imitation Baidu search drop-down automatic prompt box (with instances)

Source: Internet
Author: User

The drop-down prompt box for php + mysql + ajax Implementation of Baidu search mainly contains three files and three files in the same directory, as shown in figure

The following is the code of the three files to import the SQL file into the mysql database to modify the Database Password for their own remember Oh is UTF-8 code

Php + mysql + ajax implement Baidu search drop-down prompt box


Rpc. php fileCopy codeThe Code is as follows: <? 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 FileCopy codeThe Code is as follows: <! 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;">

<Div class = "suggestionList" id = "autoSuggestionsList">

</Div>
</Div>
</Form>
</Div>
</Body>
</Html>

SQL database autoComplete. SQL file (imported to mysql)Copy codeThe Code is as follows: -- phpMyAdmin SQL Dump
-- Version 3.3.5
Http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Date: December 09, 2010
-- Server version: 5.0.22
-- PHP version: 5.2.14
SET SQL _MODE = "NO_AUTO_VALUE_ON_ZERO ";
--
-- Database: 'test'
--
----------------------------------------------------------
--
-- Table structure '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;
--
-- The data in the table is 'countries'
--
Insert into 'countries' ('id', 'value') VALUES
(1, 'Afghanistan '),
(2, 'aringland Islands '),
(3, 'albana '),
(4, 'algera '),
(5, 'American Samoa '),
(6, 'andora '),
(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, 'bolive '),
(28, 'Bosnia and Herzegovina '),
(29, 'botyuna '),
(30, 'bouvet Island '),
(31, 'Brazil '),
(32, 'British Indian Ocean territory '),
(33, 'brunei darussalam '),
(34, 'bulgara '),
(35, 'burkina faso '),
(36, 'burundi '),
(37, 'cambodia '),
(38, 'cameron '),
(39, 'Canada '),
(40, 'cape verde '),
(41, 'cayman Islands '),
(42, 'Central African Republic '),
(43, 'Chad '),
(44, 'chile '),
(45, 'China '),
(46, 'Christmas Island '),
(47, 'cos (Keeling) Islands '),
(48, 'colorbia '),
(49, 'comoros '),
(50, 'Go '),
(51, 'Go '),
(52, 'describecatic Republic '),
(53, 'cook Island '),
(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, 'ecador '),
(66, 'Egypt '),
(67, 'El salvador '),
(68, 'equatorial Guinea '),
(69, 'eritrea '),
(70, 'estona '),
(71, 'Ethiopia '),
(72, 'falk' 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, 'guyuna '),
(95, 'haiti '),
(96, 'ard and McDonald Islands '),
(97, 'honduras '),
(98, 'Hong Kong '),
(99, 'Hungary '),
(100, 'iceland '),
(101, 'India '),
(102, 'donesia '),
(103, 'ira '),
(104, 'Iraq '),
(105, 'Land '),
(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, 'latve '),
(120, 'banon '),
(121, 'sotho '),
(122, 'loberia '),
(123, 'libyan Arab Jamahiriya '),
(124, 'liechtenstein '),
(125, 'lithuana '),
(126, 'luxembourg '),
(127, 'macao '),
(128, 'Macedonia '),
(129, 'Madagascar '),
(130, 'malawi '),
(131, 'Malaysia '),
(132, 'maldives '),
(133, 'mali '),
(134, 'malta '),
(135, 'Marshall Islands '),
(136, 'martinique '),
(137, 'mauritana '),
(138, 'mauritius '),
(139, 'mayott '),
(140, 'Mexico '),
(141, 'micronesia '),
(142, 'moldova '),
(143, 'Monaco '),
(144, 'lila '),
(145, 'montserrat '),
(146, 'morocco '),
(147, 'Your ambique '),
(148, 'myancer '),
(149, 'namiba '),
(150, 'nauru '),
(151, 'netap '),
(152, 'netherlands '),
(153, 'Netherlands Antilles '),
(154, 'new Caledonia '),
(155, 'new zealand '),
(156, 'nicaragua '),
(157, 'nier '),
(158, 'heieria '),
(159, 'niue '),
(160, 'norfolk Island '),
(161, 'Northern Mariana Islands '),
(162, 'norway '),
(163, 'oman '),
(164, 'pak '),
(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, 'rwand '),
(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 Prine e '),
(189, 'saudi Arabia '),
(190, 'senegal '),
(191, 'Serbia and Montenegro '),
(192, 'seycheles '),
(193, 'sidelleone '),
(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, 'sudo '),
(204, 'suriname '),
(205, 'svalbard and Jan Mayen Islands '),
(206, 'swaziland '),
(207, 'sweden '),
(208, 'switzerland '),
(209, 'syria '),
(210, 'Taiwan '),
(211, 'tajikistan '),
(212, 'tanzana '),
(213, 'thailand '),
(214, 'togo '),
(215, 'tokelau '),
(216, 'tangga '),
(217, 'trinidad and Tobago '),
(218, 'tunisa '),
(219, 'turkey '),
(220, 'turkmenistan '),
(221, 'turks and Caicos Islands '),
(222, 'tuvalu '),
(223, 'ugand '),
(224, 'ukraine '),
(225, 'United Arab receives '),
(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, 'zimbabw ');

Your tests on the local server must be awesome, And the principle is simple and not so troublesome.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.