صفحه ای که در سروری که توسط جاوا اسکریپت بالا فراخوانی شده است یک فایل PHP به نام "livesearch.php" است.
کد منبع در "livesearch.php" یک فایل XML را برای عناوین مطابق با رشته جستجو جستجو می کند و نتیجه را برمی گرداند:
<?php
$xmlDoc=new DOMDocument();
$xmlDoc->load("links.xml");
$x=$xmlDoc->getElementsByTagName('link');
//get the q parameter from URL
$q=$_GET["q"];
//lookup all links from the xml file if length of q>0
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) {
//find a link matching the search text
if (stristr($y->item(0)->childNodes->item(0)->nodeValue,$q)) {
if ($hint=="") {
$hint="<a href='" .
$z->item(0)->childNodes->item(0)->nodeValue .
"' target='_blank'>" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a>";
} else {
$hint=$hint . "<br /><a href='" .
$z->item(0)->childNodes->item(0)->nodeValue .
"' target='_blank'>" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a>";
}
}
}
}
}
// Set output to "no suggestion" if no hint was found
// or to the correct values
if ($hint=="") {
$response="no suggestion";
}
else {
$response=$hint;
}
//output the response
echo $response;
?>
اگر متنی از جاوا اسکریپت ارسال شده باشد (strlen($q) > 0)، موارد زیر رخ می دهد:
- یک فایل XML را در یک شیء XML DOM جدید بارگیری کنید
- حلقه همه را بزنید <title> عناصر برای یافتن مطابقت از متن ارسال شده از جاوا اسکریپت
- url و عنوان صحیح را در متغیر "$response" تنظیم می کند.
اگر بیش از یک مورد منطبق یافت شود، همه موارد مطابق به متغیر اضافه می شوند
- اگر هیچ منطبقی یافت نشد، متغیر $response روی "بدون پیشنهاد" تنظیم میشود