Note that there are some explanatory texts on larger screens.

plurals
  1. POLivesearch function with JSON
    primarykey
    data
    text
    <p>I currently have a method whereby there is an </p> <pre><code>&lt;input type="text" id="politician" name="politician" onkeyup="showResult(this.value)" value="Enter a politician's name"/&gt; </code></pre> <p>tag. In that same file that includes the input tag, there is a link to an external javascript file called ajax.js</p> <p>The contents of that file are as follows:</p> <pre><code>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 &amp;&amp; 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(); } </code></pre> <p>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.</p> <p>The livesearch.php file is as follows:</p> <pre><code>&lt;?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-&gt;load("politicians.xml"); $x=$xmlDoc-&gt;getElementsByTagName('Politicians'); $hint = ""; for($i=0; $i&lt;($x-&gt;length); $i++) { $y=$x-&gt;item($i)-&gt;getElementsByTagName('name'); $z=$x-&gt;item($i)-&gt;getElementsByTagName('url'); $w=$x-&gt;item($i)-&gt;getElementsByTagName('location'); $v=$x-&gt;item($i)-&gt;getElementsByTagName('position'); $u=$x-&gt;item($i)-&gt;getElementsByTagName('photo'); if($y-&gt;item(0)-&gt;nodeType==1) { //Find a link matching the search text if(stristr($y-&gt;item(0)-&gt;childNodes-&gt;item(0)-&gt;nodeValue,$q)) { if($hint != "") { $hint .= "&lt;br /&gt;"; } $hint .= "&lt;h1 id='poli'&gt;&lt;a id='blue' href='"; $hint .= $z-&gt;item(0)-&gt;childNodes-&gt;item(0)-&gt;nodeValue; $hint .= "'&gt;"; $hint .= $y-&gt;item(0)-&gt;childNodes-&gt;item(0)-&gt;nodeValue; $hint .= "&lt;/a&gt; &lt;br /&gt;&lt;a id='green'&gt;"; $hint .= $v-&gt;item(0)-&gt;childNodes-&gt;item(0)-&gt;nodeValue; $hint .= "&lt;/a&gt;&lt;a id='green'&gt;"; $hint .= $w-&gt;item(0)-&gt;childNodes-&gt;item(0)-&gt;nodeValue; $hint .= "&lt;/a&gt;&lt;br/&gt;&lt;img width='30' height='40' id='schmidt' src='politicians/"; $hint .= $u-&gt;item(0)-&gt;childNodes-&gt;item(0)-&gt;nodeValue; $hint .= ".png' /&gt;&lt;/h1&gt;"; } } } } // Set output to "no suggestion" if no hint were found // or to the correct values if($hint == "") echo "No suggestions"; else echo $hint; ?&gt; </code></pre> <p>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. </p> <p>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?</p> <p>Any help would be greatly appreciated!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload