Hi all,
I currently have a method whereby there is an
<input type="text" id="politician" name="politician"
onkeyup="showResult(this.value)" value="Enter a politician's name"/>
tag. In that same file that includes the input tag, there is a link to an external javascript file called ajax.js
The contents of that file are as follows:
function showResult(str)
{
if (str.length==0)
{
document.getElementById("livesearch").innerHTML="";
document.getElementById("livesearch").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("livesearch").innerHTML=xmlhttp.responseText;
document.getElementById("livesearch").style.border="1px solid #A5ACB2";
}
}
xmlhttp.open("GET","livesearch.php?politician="+str,true);
xmlhttp.send();
}
Basically, what the javascript file does is say that whenever a value is inserted into the input textbox, a request is sent to a php file called "livesearch.php" which parses the content of a hardcoded XML document called politicians.xml.
The livesearch.php file is as follows:
<?php
//Make sure we have something set before we go doing work
if (isset($_GET["politician"])){
$q = $_GET["politician"];
if ($q == "")
exit();
$xmlDoc = new DOMDocument();
$xmlDoc->load("politicians.xml");
$x=$xmlDoc->getElementsByTagName('Politicians');
$hint = "";
for($i=0; $i<($x->length); $i++)
{
$y=$x->item($i)->getElementsByTagName('name');
$z=$x->item($i)->getElementsByTagName('url');
$w=$x->item($i)->getElementsByTagName('location');
$v=$x->item($i)->getElementsByTagName('position');
$u=$x->item($i)->getElementsByTagName('photo');
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 .= "<br />";
}
$hint .= "<h1 id='poli'><a id='blue' href='";
$hint .= $z->item(0)->childNodes->item(0)->nodeValue;
$hint .= "'>";
$hint .= $y->item(0)->childNodes->item(0)->nodeValue;
$hint .= "</a> <br /><a id='green'>";
$hint .= $v->item(0)->childNodes->item(0)->nodeValue;
$hint .= "</a><a id='green'>";
$hint .= $w->item(0)->childNodes->item(0)->nodeValue;
$hint .= "</a><br/><img width='30' height='40' id='schmidt' src='politicians/";
$hint .= $u->item(0)->childNodes->item(0)->nodeValue;
$hint .= ".png' /></h1>";
}
}
}
}
// Set output to "no suggestion" if no hint were found
// or to the correct values
if($hint == "")
echo "No suggestions";
else
echo $hint;
?>
A friend of mine recently told me that JSON is a great alternative to XML because using JSON will put less strain on the server since it wont need to rely on PHP to parse the content of the XML document. The client would parse it instead.
But, I don't have enough experience with JSON to rewrite this function so that it works with a JSON document. Could anyone give me a few tips?
Any help would be greatly appreciated!