0

所以我复制/粘贴了一些用于实时搜索的代码(来自 w3schools 的代码),当用户开始输入时,会出现一个带有一些超链接单词的下拉列表。当用户单击这些超链接时,它将打开一个新页面,其中包含他们单击的特定项目。目前,这些超链接都是 .html ,我假设以下行默认在 .html 页面中打开这些页面:

$z=$x->item($i)->getElementsByTagName('url');

我的问题是:如何将其链接到 .php 页面?

下面是显示结果的 JS 代码:

<script type="text/javascript">
function showResult(str)
{
if (str.length==0)
  { 
  document.getElementById("search").innerHTML="";
  document.getElementById("search").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("search").innerHTML=xmlhttp.responseText;
    document.getElementById("search").style.border="1px solid #A5ACB2";
    }
  }
 xmlhttp.open("GET","search2.php?q="+str,true);
xmlhttp.send();
}
</script>

这是用于搜索的 PHP:

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

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

//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('commonname');
  $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>"; 
        //$hint= $y->item(0)->childNodes->item(0)->nodeValue. "<br />";
        }
      else
        {
        $hint=$hint . "<br /><a href='" . 
        $z->item(0)->childNodes->item(0)->nodeValue . 
        "' target='_blank'>" . 
        $y->item(0)->childNodes->item(0)->nodeValue . "</a>";
        //$hint= $hint.$y->item(0)->childNodes->item(0)->nodeValue. "<br />";
        }
      }
    }
  }
}

// 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;
?>
4

0 回答 0