PHP – Ajax Autosuggest Uygulaması

Bu uygulamamızda web sitelerinde kullanılan arama bölümlerinin biraz daha gelişmiş bir halini Ajax Autosuggest  (Ajax Search) ‘i öğrenecegiz. Bu örnegimizde links.xml de dosyamızda yer alan bilgileri aramamız ve bulmamızı sağlayan bir örneği yapıyoruz. Uygulama için kodlar aşağıda yer alamaktadır.Uygulma Dosyaları livesearch.js dosyamız aşağıdaki gibidir:

var xmlhttp;function showResult(str){if (str.length==0){document.getElementById("livesearch").innerHTML="";document.getElementById("livesearch").style.border="0px";return;}xmlhttp=GetXmlHttpObject()if (xmlhttp==null){alert ("Your browser does not support XML HTTP Request");return;}var url="livesearch.php";url=url+"?q="+str;url=url+"&sid="+Math.random();xmlhttp.onreadystatechange=stateChanged ;xmlhttp.open("GET",url,true);xmlhttp.send(null);}function stateChanged(){if (xmlhttp.readyState==4){document.getElementById("livesearch").innerHTML=xmlhttp.responseText;document.getElementById("livesearch").style.border="1px solid #A5ACB2";}}function GetXmlHttpObject(){if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safarireturn new XMLHttpRequest();}if (window.ActiveXObject){// code for IE6, IE5return new ActiveXObject("Microsoft.XMLHTTP");}return null;}

livesearch.php Dosyamızın kodlar.

< ?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>0if (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 textif (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 were found// or to the correct valuesif ($hint == ""){$response="no suggestion";}else{$response=$hint;}//output the responseecho $response;?>

index.html dsoyamızın kodları.

<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><input type="text" id="txt1" size="30" onKeyUp="showResult(this.value)" /><div id="livesearch"></div></form></body></html>

link

< ?xml version="1.0" encoding="ISO-8859-1"?><!-- Edited by XMLSpy® --><pages><link><title>Kültür Tv</title><url>http://www.kulturtv.com.tr</url></link><link><title>Kitap Yayınla</title><url>http://www.kitapyayinla.com</url></link><title>Serpito</title><url>http://www.serpito.com</url></pages>

sword fish

Hem yazılım, hem kendini geliştiriyor. teknoloji meraklısı... kişisel weblogu

Website - More Posts

One comment

  • 18 Eylül 2009 - 09:31 | Permalink

    bende dneyeyım olacakmı

  • Bir Cevap Yazın

    E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir

    *

    Şu HTML etiketlerini ve özelliklerini kullanabilirsiniz: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>