Simple button click counter using PHP, AJAX and JavaScript
<p>I simply just want to count the clicks of a button and save the amount of clicks to a text file, but I cannot seem to get this code to work. It does not save the amount of clicks.</p>

<p>In the HTML file I have a button that runs the JavaScript using <code>onClick</code>:</p>

<pre><code>function do() {
    if (window.XMLHttpRequest){
              xhr = new XMLHttpRequest()
    }
    else 
    {
    if (window.ActiveXObject){
              var xhr = new ActiveXObject("Microsoft.XMLHTTP");
              }
              }
            xhr.open('GET','count.php',false);
            xhr.onreadystatechange = function() {
                if( xhr.readyState === 4 &amp;&amp; xhr.status === 200 ) {
                    while(results.hasChildNodes()) {
                        results.removeChild(results.lastChild);
                    }
                    results.appendChild(document.createTextNode(xhr.responseText));
                }
            }
            xhr.send();
    }
</code></pre>

<p>In the PHP file we called, we have the following code:</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>Can you help me to find the problem in my code?<br>
How would I read the text file in another HTML page?<br>
(Just show the information of the text file.)</p>
