The HTML form above has an input field called "txt1". An event attribute for this field defines a function to be triggered by the onkeyup event.
The paragraph below the form contains a span called "txtHint". The span is used as a placeholder for data retrieved from the web server.
When a user inputs data, the function called "showHint()" is executed. The execution of the function is triggered by the "onkeyup" event. In other words: Each time a user moves the finger away from a keyboard key inside the input field, the function showHint is called.
Example explained - The JavaScript code
This is the JavaScript code, stored in the file "clienthint.js":
The showHint() function
The showHint() function above is executed every time a character is entered in the "txt1" input field.
If there is input in the input field (str.length > 0), the showHint() function executes the following:
•Calls the GetXmlHttpObject() function to create an XMLHTTP object
•Defines the URL (filename) to send to the server
•Adds a parameter (q) to the URL with the content of the input field
•Adds a random number to prevent the server from using a cached file
•Each time the readyState property changes, the stateChanged() function will be executed
•Opens the XMLHTTP object with the given URL
•Sends an HTTP request to the server
If the input field is empty, the function simply clears the content of the txtHint placeholder.
The GetXmlHttpObject() function
The showHint() function above calls a function named GetXmlHttpObject().
The purpose of the GetXmlHttpObject() function is to solve the problem of creating different XMLHTTP objects for different browsers.
The stateChanged() function
The stateChanged() function executes every time the state of the XMLHTTP object changes.
When the state changes to 4 ("complete"), the content of the txtHint placeholder is filled with the response text.
Example explained - The PHP page
The code in the "gethint.php" checks an array of names and returns the corresponding names to the client:
//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;
?>
If there is any text sent from the JavaScript (strlen($q) > 0), the following happens:
Find a name matching the characters sent from the JavaScript
If no match were found, set the response string to "no suggestion"
If one or more matching names were found, set the response string to all these names
The response is sent to the "txtHint" placeholder.
purchase structured settlements,
loan consolidation student loans,
accident no win no fee,
tax attorneys los angeles,
get auto insurance online,
online life assurance quotes,
scottsdale dui lawyer,
purchase structured settlements,
loan consolidation student loans,
accident no win no fee,
tax attorneys los angeles,
get auto insurance online,
online life assurance quotes,
scottsdale dui lawyer,