March 16, 2013
OnClick fopen fread fwrite fclose
Question by artur99
I have a image:
<img src="imagini/floo.gif" onclick="score()">
and i want “on click” to be opened a file “c.txt”.
That file’s content is:
0
And i want on every click, to be added 100 in c.txt.
I want something like this:
<img src="imagini/floo.gif" onclick="score()">
<script> function score(){ (..do file add.php ...) }</script>
And add.php:
<?php
$c = "c.txt";
$fh = fopen($c, "r");
$current = fread($fh, filesize($c));
fclose($fh);
$current++;
$fh = fopen($c, "w");
fwrite($fh,$current);
fclose($fh);
?>
What to put in place: “(..do file add.php …)” for the code to work?
Answer by Starx
You need to send an AJAX request to process the file. Here is a jQuery version of the code:
function score() {
$.post("add.php", function(data) {
//This callback executes after the request was successfull
// echo something back to read from here as `data`
console.log(data);
});
}