Note that there are some explanatory texts on larger screens.

plurals
  1. PODisplay value from text file on website
    primarykey
    data
    text
    <p>I am using AJAX to track the number of clicks of a button on my site and I would like to display that value on the page as well. Currently the tracking is working great and the number of clicks is being printed to a document called clicks.txt and I was wondering how I could go about reading the value from that text file and printing it to the page.</p> <p>I tried searching SO and Google for an answer, but I guess I can't think of how to word it properly because the answers I have found so far have been unrelated to what I'm trying to do.</p> <p>Here is the AJAX included in index.php</p> <pre><code>&lt;script type="text/javascript"&gt; function getXMLHttp() { var xmlHttp try { xmlHttp = new XMLHttpRequest(); } catch(e) { try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) { try { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) { return false; } } } return xmlHttp; } function MakeRequest() { var xmlHttp = getXMLHttp(); xmlHttp.onreadystatechange = function() { if(xmlHttp.readyState == 4) { HandleResponse(xmlHttp.responseText); } } xmlHttp.open("GET", "counter.php", true); xmlHttp.send(null); } function HandleResponse(response) { document.getElementById('ResponseDiv').innerHTML = response; } &lt;/script&gt; </code></pre> <p>Here is the external PHP(counter.php) I am using to track the button clicks:</p> <pre><code>&lt;?php $clicks = file_get_contents("clicks.txt"); $clicks++; $fp = fopen("clicks.txt", "w+"); while ( !flock($fp, LOCK_EX) ) { usleep(500000); // Delay half a second } fwrite($fp, $clicks); fclose($fp); flock($fp, LOCK_UN); ?&gt; </code></pre> <p>Thanks in advance!</p>
    singulars
    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.
    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