After j is input, the ajax effect is triggered. Data with j in the corresponding name is obtained from the background and displayed in suggestions.
The code is implemented as follows:
Ajax requires three files: one is an html form file, the other is a js core file, and the other is a php background file.
The following is an html file. When the keyboard is pressed, The showHint method is triggered. The showHint method contains the core ajax content, such as instantiation, address retrieval, data retrieval, and display.
Copy codeThe Code is as follows:
<Html>
<Head>
<Script src = "clienthint. js"> </script>
</Head>
<Body>
<Form>
First Name:
<Input type = "text" id = "txt1"
Onkeyup = "showHint (this. value)">
</Form>
<P> Suggestions: <span id = "txtHint"> </span> </p>
</Body>
</Html>
The following is the js content clienthint. js.
Copy codeThe Code is as follows:
Var xmlHttp
Function showHint (str)
{
If (str. length = 0)
{
Document. getElementById ("txtHint"). innerHTML = ""
Return
}
// Obtain the xmlHttpObject object. If it is null, the browser is prompted that ajax is not supported.
XmlHttp = GetXmlHttpObject ()
If (xmlHttp = null)
{
Alert ("Browser does not support HTTP Request ")
Return
}
// Obtain the url
Var url = "gethint. php"
Url = url + "? Q = "+ str
Url = url + "& sid =" + Math. random ()
// Callback function to execute the action
XmlHttp. onreadystatechange = stateChanged
// Open
XmlHttp. open ("GET", url, true)
XmlHttp. send (null)
}
Function stateChanged ()
{
If (xmlHttp. readyState = 4 | xmlHttp. readyState = "complete ")
{
// Insert the obtained information to txtHint.
Document. getElementById ("txtHint"). innerHTML = xmlHttp. responseText
}
}
// Obtain the xml Object
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;
}
Below is the content of php. Obtain the corresponding data based on the parameters passed in by the ajax object.
Copy codeThe Code is as follows:
<? Php
// Fill up array with names
$ A [] = "Anna ";
$ A [] = "Brittany ";
$ A [] = "Cinderella ";
$ A [] = "Diana ";
$ A [] = "Eva ";
$ A [] = "Fiona ";
$ A [] = "Gunda ";
$ A [] = "Hege ";
$ A [] = "Inga ";
$ A [] = "Johanna ";
$ A [] = "Jiqing ";
$ A [] = "Kitty ";
$ A [] = "Linda ";
$ A [] = "Nina ";
$ A [] = "Ophelia ";
$ A [] = "Petunia ";
$ A [] = "Amanda ";
$ A [] = "Raquel ";
$ A [] = "Cindy ";
$ A [] = "Doris ";
$ A [] = "Eve ";
$ A [] = "Evita ";
$ A [] = "Sunniva ";
$ A [] = "Tove ";
$ A [] = "Unni ";
$ A [] = "Violet ";
$ A [] = "Liza ";
$ A [] = "Elizabeth ";
$ A [] = "Ellen ";
$ A [] = "Wenche ";
$ A [] = "Vicky ";
// Get the q parameter from URL
$ Q = $ _ GET ["q"];
// Lookup all hints from array if length of q> 0
If (strlen ($ q)> 0)
{
$ Hint = "";
For ($ I = 0; $ I <count ($ a); $ I ++)
{
If (strtolower ($ q) = strtolower (substr ($ a [$ I], 0, strlen ($ q ))))
{
If ($ hint = "")
{
$ Hint = $ a [$ I];
}
Else
{
$ Hint = $ hint. ",". $ a [$ I];
}
}
}
}
// Set output to "no suggestion" if no hint were found
// Or to the correct values
If ($ hint = "")
{
$ Response = "no suggestion ";
}
Else
{
$ Response = $ hint;
}
// Output the response
Echo $ response;
?>