user1431627
user1431627

Reputation: 815

Problems with counting in text

I have a problem when trying to count in PHP using a button and a text file to save it. I can't see the problem in my code.

<!DOCTYPE html>
<html>
 <head>
 <meta charset="utf-8">
 <script src="http://code.jquery.com/jquery-latest.js"></script>
 </head>
 <body>
   <div id="p">Click here</div>
   <div id="results"></div>
<script>
$('#p').click(function()
{
   $('#results').load("count.php");
});
</script>
 </body>
</html>

count.php contains the following:

    <?php

    $clicks = file_get_contents("clicks.txt");
    $clicks++;

        $fp = fopen("clicks.txt", "w+");
        fwrite($fp, $clicks);
        fclose($fp);

//give the count to the user
echo "result: $clicks";

    ?>

Upvotes: 2

Views: 131

Answers (1)

rlemon
rlemon

Reputation: 17666

From the OP it looks like you want this data to be retained and accumulate for all users, so a server side component is necessary.

Here is my take on it:
HTML

<html>
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    </head>
    <div id="p">Click here</div>
    <div id="results"></div>
    <script>
    $('#p').click(function()
    {
        $.ajax("count.php", {
                complete: function(jqXHR, status) {
                    $("#results").text(jqXHR.responseText);
                }
            })
    });
    </script>
    </body>
</html>

PHP

<?php
   $clicks = intval(file_get_contents("clicks.txt"));
    $clicks++;

        $fp = fopen("clicks.txt", "w+");
        fwrite($fp, $clicks);
        fclose($fp);

//give the count to the user
echo "result: $clicks";

notice the php is the same but I now change the $clicks value to an integer after I get it from the file.

You also need to ensure the files are read/write for the server... in linux you would just do this:

sudo chown -R www-data:www-data /var/www
sudo chmod -R g+rw /var/www

to make your www/ directory (or change it to the directory your Apache(?) server uses..) read/writeable by the server (this may be overkill, you can target specific files using the same method, just remove the -R (recursive) flag).

I tested this setup locally and it works for me, goodluck!

Edit: Just being nice....

Here is a no-jQuery solution to the JS (this won't work in all browsers, specifically older IE versions, I'll let you handle these :P )

var p_btn = document.getElementById('p'),
    results = document.getElementById('results');
p_btn.addEventListener('click', function() {
    var xhr = new XMLHttpRequest();
    xhr.open('GET','count.php',false);
    xhr.onreadystatechange = function() {
        if( xhr.readyState === 4 && xhr.status === 200 ) {
            while(results.hasChildNodes()) {
                results.removeChild(results.lastChild);
            }
            results.appendChild(document.createTextNode(xhr.responseText));
        }
    }
    xhr.send();

}, false);
// enjoy!

Live Demo

Ignore the add code that gets spit back out from the server.... damn free hosts

Upvotes: 4

Related Questions