I have a site http://www.gfcf14greendream.com/ where I'm posting my programs and games. I have no problem writing code to download a program I made (click on the Programs button then SMS Sender), but I'm rather confused as to how to generate a counter. Before anything, I do not wish to use one from a website, because I'd really like to format it myself. The way I thought of doing this was through the use of a txt file, smssender.txt which simply has:
0
Then the javascript of the site which handles the overwrite of the txt file is:
$("#downbutton").click(function () {
downcounter++;
if (downcounter == 1) $("#counter").text("SMS Sender has been downloaded " + downcounter + " time...");
else $("#counter").text("SMS Sender has been downloaded " + downcounter + " times...");
$.post("http://www.gfcf14greendream.com/PHP/smssender.php", {
counter: downcounter
}, function (data) {});
});
which should call a PHP file, smssender.php, which has but 5 lines:
<?php
$counter = $_POST['counter'];
file_put_contents("/counters/smssender.txt", $counter);
?>
I wish I knew if the php page is being called at all, because the code that changes the text that indicates the download times in the page works well, but as soon as the page is refreshed, the number of downloads goes back to 0, because the 0 is obtained from this code:
var downcounter = 0;
$.get("http://www.gfcf14greendream.com/counters/smssender.txt", function (data) {
downcounter = data;
if (downcounter == 1) $("#counter").text("SMS Sender has been downloaded " + downcounter + " time...");
else $("#counter").text("SMS Sender has been downloaded " + downcounter + " times...");
});
which clearly indicates no overwrite took place since it successfully retrieves the 0 from smssender.txt (I've previously tried with 1 and 2 and it worked). So why is the code wrong? Any kind of help is truly appreciated. Thanks for your time, and Happy New Year to all!!
Update:
I tried changing the code of the javascript function to:
var txtfile = "http://www.gfcf14greendream.com/counters/smssender.txt";
$.ajax({
type:'POST',
url: 'PHP/increment.php',
data: txtfile,
success: function() {
alert("Download incremented");
$.get("http://www.gfcf14greendream.com/counters/smssender.txt", function(data){
downcounter = data;
if (downcounter == 1) $("#counter").text("SMS Sender has been downloaded " + downcounter + " time...");
else $("#counter").text("SMS Sender has been downloaded " + downcounter + " times...");
});
}
});
And added a PHP file, increment.php, which has some of the code you gave me here:
<? php
$file = $_POST['txtfile'];
$counter = intval(file_get_contents($file));
$counter++;
file_put_contents($file, $counter);
?>
But still, no luck. Would this code work at all or am I misusing references? Thank you