PDA

View Full Version : counter for clicking on a button


deaf_digit
11-15-2005, 10:32 PM
Hi..

I'm not really sure this is the best place to put this thread but here I go any road..

At a certain place on my website i have an input box placed
prompt("a question?","") ... you know the kind

This input box, of course, also has the inevitable "OK"-button on it
I need a script that keeps track of how many times the "OK"-button has been clicked (and enables me to write this "number of clicks" on my site).. I suppose it should work more or less the same way as a counter that keeps track of how many people that has visited the site..
Can anybody help me? I don't really care what language it's written in, but i suppose JavaScript or PHP would be the obvious choice??? :thumbsup:
Oh by the way.. I do have the possibility of using MySQL but I know nothing about it... guess I need pretty detailed help here :)

Please help

//deaf_digit



http://www.dingodism.tk

Velox Letum
11-16-2005, 01:28 AM
The prompts are client side in nature, thus you would have to use javascript.

deaf_digit
11-21-2005, 04:22 PM
Okay.. hmm.. so far so good..

You wouldn't by any chance happen to have a script that does the trick?
Or maybe you or someone else could write one for me?

//deaf_digit

GJay
11-21-2005, 10:04 PM
<?php
$f = fopen('counter.txt','w+');
$total=fread($f,filesize('counter.txt'));
/*if someone has clicked submit*/
if(isset($_POST['submit'])) {
$total=intval($total)++;
fwrite($f,$total);


}
fclose($f);
?>

At the top of a file...
then:

<form action='' method="post">
<input type="submit" name="submit" value="click" />
</form>
This button has been clicked <?php echo $total; ?> times.

Wherever you want it in the page

deaf_digit
11-22-2005, 01:23 AM
Wow.. that simple huh?

Thanks GJay.. I owe you one

//deaf_digit

Velox Letum
11-22-2005, 01:48 AM
That isn't for a javascript button though, that's for a form button. For a javascript button it'd have to open something.

GJay
11-22-2005, 07:34 PM
He didn't specifically request javascript, and doing it with PHP is a lot easier.
Doing it as a combination of the 2, with an XMLHttpRequest, would be rather elegant, but probably a little ott.

Velox Letum
11-23-2005, 01:03 AM
However, he was speaking of a prompt, which would have to submit a form in that case.

deaf_digit
11-24-2005, 12:07 AM
Well actually I don't really care about the language or the button style..
As long as it works.. The "prompt" thing is just the way I've made it yet. If PHP is easier that would do nicely.. But thanks to both of you..

Oh by the way.. as told earlier I don't really know much about PHP so I'm having a little trouble reading the codes of the script GJay posted.. What would I have to change to make it work and what would I have to change it into.. oh and is it possible to put the counter on one .htm page and display the result of the counting on another?

I know I'm not really the master of coding in these languages but if any of you ever need help with VB (Visual Basic) or VBA (Visual Basic Application), I'm there! :thumbsup:
I do a little python too but not a lot.. Thanks for all your help guys.. keep it coming :D

//deaf_digit

GJay
11-24-2005, 09:13 AM
the 'name' attribute of the button ('submit' in my example) needs to be the same as the if(isset($_POST['xxxxx'])... bit.
To have just the display on another page,

<?php
$f = fopen('counter.txt','w+');
$total=fread($f,filesize('counter.txt'));
fwrite($f,$total);
fclose($f);
echo $total;
?>

Where you want the total to be duisplayed

deaf_digit
11-26-2005, 04:04 AM
Thanks for the script GJay...doesn't really seem to work though or am I doing something wrong?

<?php
$f = fopen('counter.txt','w+');
$total=fread($f,filesize('counter.txt'));
/*if someone has clicked submit*/
if(isset($_POST['submit'])) {
$total=intval($total)++;
fwrite($f,$total);


}
fclose($f);
?>


<html>
<head>
<title></title>
</head>
<body>
<form action='' method="post">
<input type="submit" name="submit" value="click" />
</form>
This button has been clicked <?php echo $total; ?> times.
</body>
</html>



Shouldn't this work? This is just a page I've made for the sole purpose of making this work.. Is something wrong with it? No matter how many times I click the button it just displays: "This button has been clicked times"...As if it is unable to yield the number of clicks?...
Thanks again though :)

GJay
11-26-2005, 11:58 AM
Check that PHP has permissions to create/read/write the text file (so first run the script, then see if the file was created, if it was, see if there's anything in it, if so then I'm not sure....

deaf_digit
11-29-2005, 12:07 AM
It seems the file wasn't created. I opened the site (above), clicked the button and then searched my computer for files called/containing "counter" and "counter.txt" - neither existed - so I guess my conclusion is that PHP does not have these permissions - how do I change this?

//deaf_digit