Jump to content

AJAX Live Search


slimboyfatz32

Recommended Posts

Hi all , new to the forums so a big 'hello' to everyone !!!I have recently being playing about with the PHP and AJAX live search tutorial on the W3School site , all is working well , and top marks to the coder!!My example can be seen here (in test form) http://www.atrium-improvements.co.uk/search.htmlThe only tweak i would like to do (and this is where pro-coders could help) is to have it act like the 'ASK.com' search function (ie ; it will only follow on from your first letter) .... example when you use the search function in my link above and start typing 'website design' it brings up 'sandwich shops' also (which i dont want because i have'nt even pressed S).... i'm guessing the script is looking at the 'wich' part of the word.Thats what i was hoping for help with , many thanks

Link to comment
Share on other sites

The problem appears to be with the way your PHP handles the request. Check out the following:http://www.atrium-improvements.co.uk/livesearch.php?q=dThis returns a list of items that have the letter "d" in them somewhere rather than a list of items that start with the letter "d".
Thanks a million Jesh for replying , like you mentioned above that is where the problem lies. I do not want the function to return a list with a specific letter in it , but one that returns a list with only the Starting letter ... (i.e; googles autocomplete feature etc....) but i am not sure how to , your help would be greatly appreciated !!!
Link to comment
Share on other sites

But I would suspect that in the LIKE part of your SQL query in the PHP page, you have two wildcards (%) surrounding the search term variable, whereas for what you want you only need one after the variable.

Link to comment
Share on other sites

But I would suspect that in the LIKE part of your SQL query in the PHP page, you have two wildcards (%) surrounding the search term variable, whereas for what you want you only need one after the variable.
cheers synook , but i am still puzzled , can you elaborate further , i have posted the PHP code below ..............
<?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 text  if (stristr($y->item(0)->childNodes->item(0)->nodeValue,$q))   {   if ($hint=="")    {    $hint="<a href='" .     $z->item(0)->childNodes->item(0)->nodeValue .     "' target='_self'>" .     $y->item(0)->childNodes->item(0)->nodeValue . "</a>";    }   else    {    $hint=$hint . "<br /><a href='" .     $z->item(0)->childNodes->item(0)->nodeValue .     "' target='_self'>" .     $y->item(0)->childNodes->item(0)->nodeValue . "</a>";    }   }  } }}// Set output to "Business Type Not Found" if no hint were found// or to the correct valuesif ($hint == "") { $response="Business Type Not Found"; }else { $response=$hint; }  //output the responseecho $response;?>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...