Dear All,
With help from you all, I have a successful instant search on the current draft of our website. I'd love help with three minor issues of following convention:
1) Clicking on your link of choice brings about its appearance in another window -- that makes going back difficult and confusing. What can I do to add to the script (or php) to make the new link appear in the same window?
2) At the moment, to move out of the search box and down the list of choices one uses the tab key (or even worse the shift-tab to go up). How can I "bind" the script (or do whatever) to allow the down and up keys to move the focus down and up (and stopping at the bottom and top) just as it happens in Google.
3) Following up on #2, it also would be lovely, as you arrow down and up, if the choice that you're on would "light up" in some way just as it does when you hover with the mouse. Would I add something to the css in the html section of the code?
You can see the problems in action with fake data at:
http://www.woofwoofwoof.org/is01/search.htm
I'm a relative newbie at java and php. Thank you so much for your time and expertise. -- Dr C.
Here's the relevant script and php code:
<script type="text/javascript">
function showResult(str)
{
if (str.length==0)
{
document.getElementById("livesearch").innerHTML="";
document.getElementById("livesearch").style.border="0px"
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
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.fontFamily="Arial";
}
}
xmlhttp.open("GET","livesearch.php?q="+str,true);
xmlhttp.send();
}
</script>
And the PHP:
<?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 were found
// or to the correct values
if ($hint=="")
{
$response="no suggestion";
}
else
{
$response=$hint;
}
//output the response
echo $response;
?>
--end --