Javascript 为什么我会得到;PHP警告:尝试读取属性";“节点类型”;论空;此PHP AJAX搜索出错?
我正在尝试基于以下示例创建实时搜索功能: 我正在搜索一个包含1000个不同项目的xml文件 每当我在文本框中键入任何内容时,我都会收到这个错误,看起来是1000次: 警告:试图在第25行的liveSearch.php中读取null上的属性“nodeType” 代码中的对应行如下所示:Javascript 为什么我会得到;PHP警告:尝试读取属性";“节点类型”;论空;此PHP AJAX搜索出错?,javascript,php,ajax,xml,Javascript,Php,Ajax,Xml,我正在尝试基于以下示例创建实时搜索功能: 我正在搜索一个包含1000个不同项目的xml文件 每当我在文本框中键入任何内容时,我都会收到这个错误,看起来是1000次: 警告:试图在第25行的liveSearch.php中读取null上的属性“nodeType” 代码中的对应行如下所示: if ($y->item(0)->nodeType==1) { 我可以通过inspect的network选项卡看到它正在调用php文件并传递get请求,比如liveSearch.php
if ($y->item(0)->nodeType==1) {
我可以通过inspect的network选项卡看到它正在调用php文件并传递get请求,比如liveSearch.php?q=sea
我真的不明白我为什么会出现这个错误,以及如何修复它。任何帮助都将不胜感激
XML文件如下所示(小摘录):
1.
博克斯特
多洛甜甜圈。莫比·维勒·莱克图斯在弗林尼拉·朗库斯广场。莫里斯·埃尼姆·利奥,朗库斯·塞德,前庭坐着埃米特,库库斯·艾德,turpis。整型阿利奎特肌,痉挛性前肢肌,奥古斯斜颈肌,肌力肌力肌力肌力肌力肌力肌力肌力肌力肌力肌力肌力肌力肌力肌力肌力肌力肌力肌力肌力肌力肌力肌力肌力肌力肌力肌力肌力肌力肌力肌力肌力肌力。毛里斯·拉齐尼亚·萨皮恩·奎斯·利伯罗。
https://robohash.org/illoitaquenam.png?size=50x50&set=set1
";
}否则{
$hint=$hint.“
”;
}
}
}
}
}
//如果未找到提示,则将输出设置为“无建议”
//或设置为正确的值
如果($hint==“”){
$response=“无建议”;
}否则{
$response=$hint;
}
//输出响应
回音$应答;
您最初选择的是项目标题
标记,而不是行
标记
$x=$xmlDoc->getElementsByTagName('row');
<h3>Search</h3>
<!-- Script for the live search-->
<script>
function showHint(str){
//if nothing has been typed, then set the suggestion to nothing
if (str.length == 0){
document.getElementById('txtHint').innerHTML ='';
return
}else{
//Makes the AJAX request
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function(){
if(this.readyState ==4 && this.status == 200){
document.getElementById('txtHint').innerHTML = this.responseText;
}
}
xmlhttp.open("GET", "liveSearch.php?q="+str , true)
xmlhttp.send();
}
}
</script>
<input type="text" name = "searchQuery" placeholder="Search..." onkeyup="showHint(this.value)">
<div id="txtHint"></div>
$q = $_GET['q'];
$txtHint = "";
//loads in the XML document
$xmlDoc=new DOMDocument();
$xmlDoc->load("items.xml");
$x=$xmlDoc->getElementsByTagName('itemTitle');
//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('itemTitle');
$z=$x->item($i)->getElementsByTagName('itemImage');
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 was found
// or to the correct values
if ($hint=="") {
$response="no suggestion";
} else {
$response=$hint;
}
//output the response
echo $response;
$x=$xmlDoc->getElementsByTagName('row');