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:

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(){ ( file add.php ...) }</script>

And add.php:

$c = "c.txt";
$fh = fopen($c, "r");
$current = fread($fh, filesize($c));
$fh = fopen($c, "w");

What to put in place: “( 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`

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+


Please fill the form - I will response as fast as I can!