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;
You initially select theitemTitle
tag instead of therow
tag.
$x=$xmlDoc->getElementsByTagName('row');
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
JavaScript is a multi-paradigm language that supports event-driven, functional, and mandatory (including object-oriented and prototype-based) programming types. Originally JavaScript was only used on the client side. JavaScript is now still used as a server-side programming language. To summarize, we can say that JavaScript is the language of the Internet.
https://www.javascript.com/
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.