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