Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP Example AJAX Live Search
    primarykey
    data
    text
    <p>i'm relatively new when it comes to creating web pages that use PHP and XML but i am interested in somthine that i saw at W3S Schools. i want to create a AJAX live search to the one that is shows in there example page but first i need help learning how to make it run.(<a href="http://www.w3schools.com/php/php_ajax_livesearch.asp" rel="nofollow">http://www.w3schools.com/php/php_ajax_livesearch.asp</a>) i copy pasted the three code files that are in the website and when i click the html file all i get is an empty form box. Do i need to somehow link this with MySql? if so how do i got about doing this?</p> <p>index.html:</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;script&gt; 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?q="+str,true); xmlhttp.send(); } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form&gt; &lt;input type="text" size="30" onkeyup="showResult(this.value)"&gt; &lt;div id="livesearch"&gt;&lt;/div&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>livesearch.php : </p> <pre><code>&lt;?php $xmlDoc=new DOMDocument(); $xmlDoc-&gt;load("links.xml"); $x=$xmlDoc-&gt;getElementsByTagName('link'); //get the q parameter from URL $q=$_GET["q"]; //lookup all links from the xml file if length of q&gt;0 if (strlen($q)&gt;0) { $hint=""; for($i=0; $i&lt;($x-&gt;length); $i++) { $y=$x-&gt;item($i)-&gt;getElementsByTagName('title'); $z=$x-&gt;item($i)-&gt;getElementsByTagName('url'); 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;a href='" . $z-&gt;item(0)-&gt;childNodes-&gt;item(0)-&gt;nodeValue . "' target='_blank'&gt;" . $y-&gt;item(0)-&gt;childNodes-&gt;item(0)-&gt;nodeValue . "&lt;/a&gt;"; } else { $hint=$hint . "&lt;br /&gt;&lt;a href='" . $z-&gt;item(0)-&gt;childNodes-&gt;item(0)-&gt;nodeValue . "' target='_blank'&gt;" . $y-&gt;item(0)-&gt;childNodes-&gt;item(0)-&gt;nodeValue . "&lt;/a&gt;"; } } } } } // Set output to "no suggestion" if no hint were found // or to the correct values if ($hint=="") { $response="no suggestion"; } else { $response=$hint; } //output the response echo $response; ?&gt; </code></pre> <p>links.xml : </p> <pre><code>&lt;!-- Edited by XMLSpy® --&gt;&lt;pages&gt;&lt;link&gt;&lt;title&gt;HTML a tag&lt;/title&gt;&lt;url&gt;http://www.w3schools.com/tags/tag_a.asp&lt;/url&gt;&lt;/link&gt;&lt;link&gt;&lt;title&gt;HTML br tag&lt;/title&gt;&lt;url&gt;http://www.w3schools.com/tags/tag_br.asp&lt;/url&gt;&lt;/link&gt;&lt;link&gt;&lt;title&gt;CSS background Property&lt;/title&gt;&lt;url&gt;http://www.w3schools.com/cssref/css3_pr_background.asp&lt;/url&gt;&lt;/link&gt;&lt;link&gt;&lt;title&gt;CSS border Property&lt;/title&gt;&lt;url&gt;http://www.w3schools.com/cssref/pr_border.asp&lt;/url&gt;&lt;/link&gt;&lt;link&gt;&lt;title&gt;JavaScript Date Object&lt;/title&gt;&lt;url&gt;http://www.w3schools.com/jsref/jsref_obj_date.asp&lt;/url&gt;&lt;/link&gt;&lt;link&gt;&lt;title&gt;JavaScript Array Object&lt;/title&gt;&lt;url&gt;http://www.w3schools.com/jsref/jsref_obj_array.asp&lt;/url&gt;&lt;/link&gt;&lt;/pages&gt; </code></pre> <p>Thanks for any help</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