javascript - Why do I get "PHP Warning: Attempt to read property "nodeType" on null" error on this PHP AJAX search?
I am attempting to create a live search function based on this w3schools example: https://www.w3schools.com/php/php_ajax_livesearch.asp I am searching through an xml file with 1000 different items.
Whenever I type anything into the textbox, I get this error what looks to be 1000 times:
Warning: Attempt to read property "nodeType" on null in liveSearch.php on line 25
with the corresponding line in the code being this:
if ($y->item(0)->nodeType==1) {
I can see through the network tab of inspect that it is calling the php file and passing over the get request such as this liveSearch.php?q=sea
I really do not understand why I am getting this error and how I can fix it. Any help would be appreciated.
The XML file looks like this (small excerpt):
<?xml version="1.0" encoding="UTF-8"?>
<root>
<row>
<itemID>1</itemID>
<itemTitle>Boxster</itemTitle>
<itemDescription>Donec ut dolor. Morbi vel lectus in quam fringilla rhoncus. Mauris enim leo, rhoncus sed, vestibulum sit amet, cursus id, turpis. Integer aliquet, massa id lobortis convallis, tortor risus dapibus augue, vel accumsan tellus nisi eu orci. Mauris lacinia sapien quis libero.</itemDescription>
<itemImage>https://robohash.org/illoitaquenam.png?size=50x50&set=set1</itemImage>
<auctionID>4</auctionID>
</row>
<row>
<itemID>2</itemID>
<itemTitle>Santa Fe</itemTitle>
<itemDescription>Mauris lacinia sapien quis libero. Nullam sit amet turpis elementum ligula vehicula consequat. Morbi a ipsum. Integer a nibh.</itemDescription>
<itemImage>https://robohash.org/eveniettemporeipsa.png?size=50x50&set=set1</itemImage>
<auctionID>3</auctionID>
</row>
search.phtml
<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>
livesearch.php
$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;
Answer
Solution:
You initially select the itemTitle
tag instead of the row
tag.
$x=$xmlDoc->getElementsByTagName('row');
Source