AJAX can be used to create more user-friendly and interactive searches.
The Below example will demonstrate a live search, where you get search results while you type.
Live search has many benefits compared to traditional searching:
Search for a QcTutorials page in the input field below:
When a user types a character in the input field above, the function "showResult()" is executed. The function is triggered by the "onkeyup" event:
Source code explanation:
If the input field is empty (str.length==0), the function clears Content of the livesearch placeholder and exits the function.
If the input field is not empty, the showResult() function executes the following:
The page on the server called by the JavaScript above is a PHP file called "livesearch.html".
The source code in "livesearch.html" searches an XML file for titles matching the search string and returns the Output Will Be :
If there is any text sent from the JavaScript (strlen($q) > 0), the following happens:
Your Query was successfully sent!