AJAX can be used for a more user-friendly and interactive search.
AJAX Live Search
In this example we will demonstrate a live search, where you get search results while you type.
Live search has many benefits compared to traditional searching:
- Results are shown as you type
- Results narrow as you continue typing
- If results become too narrow, remove characters to see a broader result
Search for a W3Schools page in the input field below:
In the example above, the results are found in an XML document (links.xml). To make this example small and simple, only eight results are available.
Example Explained – The HTML page
The HTML page contains a link to an external JavaScript, some style definitions, an HTML form, and a div element:
| <html> <head> <script type="text/javascript" src="livesearch.js"></script> <style type="text/css"> #livesearch { margin:0px; width:194px; } #txt1 { margin:0px; } </style> </head> <body> <form> </body> |
The HTML form works like this:
- An event is triggered when the user presses, and releases a key in the input field
- When the event is triggered, the function showResult() is executed
- The <div id="livesearch"> is a placeholder for the data returned from the showResult() function
Example Explained – The JavaScript code
This is the JavaScript code stored in the file "livesearch.js":
| var xmlhttp;
function showResult(str) function stateChanged() function GetXmlHttpObject() |
The GetXmlHttpObject() function is the same as in the PHP AJAX Suggest chapter.
The showResult() Function
This function executes every time a character is entered in the input field. If there is no input in the text field (str.length == 0), the function sets the return field to empty and removes the border around it. However, if there is any input in the text field, the 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
The stateChanged() Function
This 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, and a border is set around the field.
Example Explained – The PHP page
The PHP page called by the JavaScript code is called "livesearch.php".
The code searches an XML file for titles matching the search string and returns the result as HTML:
| <?php $xmlDoc = new DOMDocument(); $xmlDoc->load("links.xml"); $x=$xmlDoc->getElementsByTagName(‘link’); //get the q parameter from URL //lookup all links from the xml file if length of q>0 // Set output to "no suggestion" if no hint were found //output the response |
If there is any text sent from the JavaScript (strlen($q) > 0), the following happens:
- PHP creates an XML DOM object of the "links.xml" file
- Loops through allĀ <title> elements to find titles that match the text sent from the JavaScript
- Sets the correct link and title in the "$response" variable. If more than one match is found, all matches are added to the variable
- If no matches are found, the $response variable is set to "no suggestion"
- Output the $respone variable to the "livesearch" placeholder
