PHP and AJAX imitation Live and Google search box

Source: Internet
Author: User
Tags html form html page http request php and

PHP and AJAX imitation Live and Google search box

Ajax Live Search
In the following example of Ajax, we will show the search for life, where the server gets the search results at the same time as the user type.

Live Search has many benefits, compared to traditional search:

The match results indicate that you typed
Result narrow you continue typing
If the result becomes too narrow, delete the character to see a wider result

This example contains four Web pages:

A simple HTML form
A JavaScript
A PHP Web page
XML file
In this example, the results are found in an XML file (Links.xml). To make this example small and simple, there are only 8 available results.


-------------------------------------------------- ------------------------------

In an HTML form
This is an HTML page. It contains a simple HTML form, style table, and links to a javascript:

<form>
<input type= "text" id= "txt1" size= ""
onkeyup= "Showresult (this.value)" >
<div id= "LiveSearch" ></div>
</form>
   
   

For example the explanation-the H tml form
As you can see, the HTML page contains a simple HTML form for the input field called "Txt1".

The form of the work is like this:

Event triggers when the user presses and publishes key areas of input
When the event triggers a function requiring showresult () to be executed.
The following form is a <div> so-called "LiveSearch". This is the data Showresult () function returned as a placeholder.

-------------------------------------------------- ------------------------------

The JavaScript
JavaScript code is stored in "Livesearch.js" and related HTML files:

var xmlHttp
function Showresult (str)
{
if (str.length==0)
 { 
 document.getElementById ("LiveSearch").
 Innerhtml= "";
 document.getElementById ("LiveSearch").
 style.border= "0px";
 Return
 }
Xmlhttp=getxmlhttpobject ()
if (xmlhttp==null)
 {
 alert (' Browser does not support HTTP Request ')
 return
 }
var url= "livesearch.php"
url=url+ "q=" +str url=url+
"&sid=" +math.random ()
xmlhttp.onreadystatechange=statechanged 
Xmlhttp.open ("Get", Url,true)
xmlhttp.send (null)
} 

function statechanged () 
{ 
if (xmlhttp.readystate==4 | | xmlhttp.readystate== "complete")
 { 
 document.getElementById ("LiveSearch").
 Innerhtml=xmlhttp.responsetext;
 document.getElementById ("LiveSearch").
 Style.border= "1px solid #A5ACB2";
 } 
function Getxmlhttpobject ()
{
var xmlhttp=null;
Try
 {
 //Firefox, Opera 8.0+, Safari
 xmlhttp=new XMLHttpRequest ();
 }
catch (E)
 {
 //Internet Explorer
 try
  {
  xmlhttp=new activexobject ("msxml2.xmlhttp");
  }
 catch (E)
  {
  xmlhttp=new activexobject ("Microsoft.XMLHTTP");
  }
return xmlHttp;
}
The back of the PHP program we just return to the data on the line here is not much to write after our site wrote a lot of related articles.

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.