Successful Ajax instances
Demo1.php
<! 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> untitled document </title>
<Script language = "JavaScript">
VaR XMLHTTP = NULL;
Function getxmlhttpobject ()
{
Try {
XMLHTTP = new XMLHttpRequest ();
} Catch (e ){
Try {
XMLHTTP = new activexobject ("msxml2.xmlhttp ");
} Catch (e ){
XMLHTTP = new activeobject ("Microsoft. XMLHTTP ");
}
}
Return XMLHTTP;
}
Function showhint (STR)
{
If (Str. Length = 0)
{
Document. getelementbyid ("suggestion"). innerhtml = "";
// Return;
}
XMLHTTP = getxmlhttpobject ();
If (XMLHTTP = NULL)
{
Alert ("Brower does not support HTTP request ");
// Return;
}
VaR url = "demo11.php? Q = "+ STR +" & id = "+ math. Random ();
// Alert (URL );
XMLHTTP. Open ("get", URL, true );
XMLHTTP. onreadystatechange = statechanged;
// Alert (XMLHTTP. readystate );
XMLHTTP. Send (null );
}
Function statechanged ()
{
If (XMLHTTP. readystate = 4)
{
// Alert (XMLHTTP. responsetext );
Document. getelementbyid ("suggestion"). innerhtml = XMLHTTP. responsetext;
}
}
</SCRIPT>
</Head>
<Body>
User name: <input type = "text" name = "user" id = "user" onkeyup = "showhint (this. Value)"/> <br/>
Suggestions: <span id = "suggestion"> </span>
</Body>
</Html>
Demo11.php
<! 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> untitled document </title>
</Head>
<Body>
<? 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 [] = "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"];
// Print_r ($ );
// Lookup all hints from array if length of q> 0
If (strlen ($ q)> 0)
{
// Echo count ($ );
$ Hint = "";
For ($ I = 0; $ I <count ($ A); $ I ++)
{
// Echo "the first line". strtolower ($ q). "<br> ";
// Echo "second line". substr ($ A [$ I], 0, strlen ($ Q). "<br> ";
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;
?>
</Body>
</Html>