Easy Tutorial
❮ Php Imagecharup Func Math Cos ❯

PHP Example - AJAX Real-Time Search


AJAX provides users with a more friendly and interactive search experience.


AJAX Live Search

In the following example, we will demonstrate a real-time search that provides results as you type.

Real-time search has many advantages over traditional search:

Enter "HTML" in the text box below to search for pages containing HTML:

The results in the above example are found in an XML file (links.xml). To keep this example small and simple, we only provide 6 results.


Example Explanation - HTML Page

When a user types characters in the input box above, the "showResult()" function is executed. This function is triggered by the "onkeyup" event:

<html>
<head>
<script>
function showResult(str)
{
    if (str.length==0)
    { 
        document.getElementById("livesearch").innerHTML="";
        document.getElementById("livesearch").style.border="0px";
        return;
    }
    if (window.XMLHttpRequest)
    {// IE7+, Firefox, Chrome, Opera, Safari browsers execute
        xmlhttp=new XMLHttpRequest();
    }
    else
    {// IE6, IE5 browsers execute
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            document.getElementById("livesearch").innerHTML=xmlhttp.responseText;
            document.getElementById("livesearch").style.border="1px solid #A5ACB2";
        }
    }
    xmlhttp.open("GET","livesearch.php?q="+str,true);
    xmlhttp.send();
}
</script>
</head>
<body>

<form>
<input type="text" size="30" onkeyup="showResult(this.value)">
<div id="livesearch"></div>
</form>

</body>
</html>

Source Code Explanation:

If the input box is empty (str.length==0), the function clears the content of the livesearch placeholder and exits.

If the input box is not empty, showResult() performs the following steps:


PHP File

The server page called by the JavaScript above is a PHP file named "livesearch.php".

The source code in "livesearch.php" searches the XML file for titles matching the search string and returns the results:

<?php
$xmlDoc=new DOMDocument();
$xmlDoc->load("links.xml");

$x=$xmlDoc->getElementsByTagName('link');

// Get the value of parameter q from the URL
$q=$_GET["q"];

// If q parameter exists, search data in the XML file
if (strlen($q)>0)
{
    $hint="";
    for($i=0; $i<($x->length); $i++)
    {
        $y=$x->item($i)->getElementsByTagName('title');
        $z=$x->item($i)->getElementsByTagName('url');
if ($y->item(0)->nodeType == 1) {
    // Found a link matching the search
    if (stristr($y->item(0)->childNodes->item(0)->nodeValue, $q)) {
        if ($hint == "") {
            $hint = "&lt;a href='" . 
                    $z->item(0)->childNodes->item(0)->nodeValue . 
                    "' target='_blank'>" . 
                    $y->item(0)->childNodes->item(0)->nodeValue . "</a>";
        } else {
            $hint = $hint . "<br />&lt;a href='" . 
                    $z->item(0)->childNodes->item(0)->nodeValue . 
                    "' target='_blank'>" . 
                    $y->item(0)->childNodes->item(0)->nodeValue . "</a>";
        }
    }
}

// If no matches are found, return "no suggestion"
if ($hint == "") {
    $response = "no suggestion";
} else {
    $response = $hint;
}

// Output the result
echo $response;
?>

If JavaScript sends any text (i.e., strlen($q) > 0), the following occurs:

❮ Php Imagecharup Func Math Cos ❯