I am using Chrome and for debug the Chrome's debugger.
Doesnt matter what iam trying i always get responseXML NULL and ofcourse i got the error "Cannot read property 'documentElement' of null". I am trying to make a searchbar which will put the results in a dropdown menu.
Here is my JS code:
/*Javascript for search*/
var xmlHttp = createXmlHttpRequestObject();
function createXmlHttpRequestObject()
{
var xmlHttp;
if(window.ActiveXObject)
{
try
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{
xmlHttp = false;
}
}else
{
try
{
xmlHttp = new XMLHttpRequest();
}
catch(e)
{
xmlHttp = false;
}
}
if(!xmlHttp)
alert ("Cant create that object :C");
else
return xmlHttp;
}
function Load()
{
if(xmlHttp.readyState == 0 || xmlHttp.readyState == 4)
{
result = encodeURIComponent(document.getElementById('searchBar').value);
xmlHttp.open("GET","searchQuery.php?searchInput="+result,true);
xmlHttp.onreadystatechange = handleServerResponse;
xmlHttp.send(null)
}else
{
setTimeout('Load()',1000);
}
}
function handleServerResponse()
{
if(xmlHttp.readyState == 4)
{
if(xmlHttp.status == 200)
{
alert(xmlHttp.responseXML);
xmlResponse = xmlHttp.responseXML;
xmlDocumentElement = xmlResponse.documentElement;
message = xmlDocumentElement.firstChild.data;
message = message.split(',');
for(i=0; i<message.length; i++)
{
s += '<li>'+message[i]+'</li>';
}
document.getElementById('searchResult').innerHTML= s;
setTimeout('Load()',1000);
}else
{
alert('Something went wrong!'+xmlHttp.status);
}
}
}
window.addEventListener('load', Load, false);
And here is my PHP code:
<?php
header('Content-Type: text/xml');
require_once('connect.php');
$dbc = mysqli_connect(host,user,pw,db) or die ("Error connecting!");
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';
echo '<response>';
$searchInput = $_GET['searchInput'];
if($searchInput == ""){ echo "Waiting"; }else
{
$queryInsert = "SELECT videoName from videotable WHERE videoName LIKE '$searchInput%' LIMIT 4";
$queryResult = mysqli_query($dbc,$queryInsert);
if(!$queryResult){echo "Failed to Query";}else
{
$result = "";
while($row = mysqli_fetch_assoc($queryResult))
{
$result .= $row['videoName'].",";
}
echo $result;
}
}
echo '</response>';
?>
Still learning XML
No comments:
Post a Comment