Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>set up your AJAX call to the php script within a JS function, and the last line of the function should be <code>setTimeout(functionname, 10000);</code></p> <p>oh, and, you'll need to call the function the first time somewhere, then it will continue to run...so:</p> <pre><code>&lt;html&gt;&lt;head&gt; &lt;script&gt; function update(){ //your ajax here setTimeout(update,10000); } update(); &lt;/script&gt;&lt;/head&gt; &lt;body&gt;&lt;div id="theDivToUpdate"&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt; </code></pre> <p>Here's some sample code to show you how you could pass JSON or XML and have the HTML page handle it. Here's the PHP (here the data is just hardcoded, but in your case you'd dynamically populate):</p> <pre><code>&lt;?php $format=$_REQUEST['format']; if($format=="json"){ header('Content-type: application/json'); header('Cache-Control: no-cache, must-revalidate'); echo "{\"status\":\"ok\",\"results\":[{\"first\":\"MyFirst\",\"last\":\"MyLast\",\"company\":\"Big Company\"},{\"first\":\"YourFirst\",\"last\":\"YourLast\",\"company\":\"Another Company\"},{\"first\":\"John\",\"last\":\"Doe\",\"company\":\"What Company?\"},{\"first\":\"Jane\",\"last\":\"Doe\",\"company\":\"Stackoverflow\"}]}"; } else if($format=="xml"){ header("Content-type: text/xml"); header('Cache-Control: no-cache, must-revalidate'); echo "&lt;ROOT&gt;&lt;STATUS&gt;ok&lt;/STATUS&gt;&lt;RESULTS&gt;&lt;RESULT&gt;&lt;FIRST&gt;MyFirst&lt;/FIRST&gt;&lt;LAST&gt;MyLast&lt;/LAST&gt;&lt;COMPANY&gt;Big Company&lt;/COMPANY&gt;&lt;/RESULT&gt;&lt;RESULT&gt;&lt;FIRST&gt;YourFirst&lt;/FIRST&gt;&lt;LAST&gt;YourLast&lt;/LAST&gt;&lt;COMPANY&gt;Another Company&lt;/COMPANY&gt;&lt;/RESULT&gt;&lt;RESULT&gt;&lt;FIRST&gt;John&lt;/FIRST&gt;&lt;LAST&gt;Doe&lt;/LAST&gt;&lt;COMPANY&gt;What Company?&lt;/COMPANY&gt;&lt;/RESULT&gt;&lt;RESULT&gt;&lt;FIRST&gt;Jane&lt;/FIRST&gt;&lt;LAST&gt;Doe&lt;/LAST&gt;&lt;COMPANY&gt;Stackoverflow&lt;/COMPANY&gt;&lt;/RESULT&gt;&lt;/RESULTS&gt;&lt;/ROOT&gt;"; } else{ echo "You need to supply the \'format\' parameter (json or xml)"; } ?&gt; </code></pre> <p>and then here is a sample html page:</p> <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt; &lt;title&gt;Untitled Document&lt;/title&gt; &lt;script&gt; function getJSON(){ var xmlhttp; xmlhttp=((window.XMLHttpRequest)?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP")); xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 &amp;&amp; xmlhttp.status==200) { var res=xmlhttp.responseText; //THE JSON STRING IS TEXT res=JSON.parse(res); //THIS CONVERTS THE JSON INTO OBJECTS THAT CAN BE ACCESSED WITH DOT NOTATION if(res.status=="ok"){ newtable="&lt;table&gt;&lt;tr&gt;&lt;th&gt;First&lt;/th&gt;&lt;th&gt;Last&lt;/th&gt;&lt;th&gt;Company&lt;/th&gt;&lt;/tr&gt;"; for(i=0;i&lt;res.results.length;i++){ newtable+="&lt;tr&gt;&lt;td&gt;"+res.results[i].first+"&lt;/td&gt;&lt;td&gt;"+res.results[i].last+"&lt;/td&gt;&lt;td&gt;"+res.results[i].company+"&lt;/td&gt;&lt;/tr&gt;"; } newtable+="&lt;/table&gt;"; document.getElementById('resultDiv').innerHTML=newtable; } } } xmlhttp.open("GET","thePHPFile.php?format=json",true); xmlhttp.send(); } function getXML(){ var xmlhttp; xmlhttp=((window.XMLHttpRequest)?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP")); xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 &amp;&amp; xmlhttp.status==200) { res=xmlhttp.responseXML; firstNameFields=res.documentElement.getElementsByTagName("FIRST"); //THESE LINES GRAB ALL OF THE NODES WITH THE INDICATED NAME AND HOLD THEM IN ARRAYS lastNameFields=res.documentElement.getElementsByTagName("LAST"); companyFields=res.documentElement.getElementsByTagName("COMPANY"); newtable="&lt;table&gt;&lt;tr&gt;&lt;th&gt;First&lt;/th&gt;&lt;th&gt;Last&lt;/th&gt;&lt;th&gt;Company&lt;/th&gt;&lt;/tr&gt;"; for(i=0;i&lt;firstNameFields.length;i++){ newtable+="&lt;tr&gt;&lt;td&gt;"+firstNameFields[i].firstChild.nodeValue+"&lt;/td&gt;&lt;td&gt;"+lastNameFields[i].firstChild.nodeValue+"&lt;/td&gt;&lt;td&gt;"+companyFields[i].firstChild.nodeValue+"&lt;/td&gt;&lt;/tr&gt;"; } newtable+="&lt;/table&gt;"; document.getElementById('resultDiv').innerHTML=newtable; } } xmlhttp.open("GET","thePHPFile.php?format=xml",true); xmlhttp.send(); } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;input type="button" onclick="getJSON()" value="Retrieve and Parse JSON" /&gt; &lt;input type="button" onclick="getXML()" value="Retrieve and Parse XML" /&gt; &lt;div id="resultDiv"&gt;&lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
 

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