I'm making a webpage that (mostly) lets users increment a counter. I can read the file with jQuery .load() just fine, but writing to it is proving difficult.
Relevant code:
HTML
<button onclick="add();">increment</button>
<p>counter value: <span id="c"></span></p>
<script src="https://code.jquery.com/jquery-3.6.3.min.js" integrity="sha256-pvPw+upLPUjgMXY0G+8O0xUf+/Im1MZjXxxgOcBQBXU=" crossorigin="anonymous"></script>
<script>
$(function() {
$("#c").load("c.txt");
});
function add() {
$.get("add.php");
$("#c").load("c.txt");
}
</script>
add.php
<?php
$file = fopen("c.txt", "w");
$c = fread($file, filesize("c.txt"));
fwrite($file, (int)$c + 1);
fclose($file);
?>
c.txt
0 or 47 or your favorite positive integer
I'm not sure what I'm doing wrong here.