se1zure Posted January 21, 2007 Share Posted January 21, 2007 HOw woult I go about keeping track of the number of times a link/submit button has been clicked? I want it to be very simple,a nd have the data be stored in a text file.I can't seemt o get the submit form to add 1 to the count without using a second php page. (otherwise, simply refreshing the page adds one to the count)In other words, how can I make it so when I click a button (in a form), it runs a php script, but not until the button is clicked. ALl without leaving that page (refreshign is okay, but leaving the page is not). Quote Link to comment Share on other sites More sharing options...
PC Nerd Posted January 21, 2007 Share Posted January 21, 2007 well if there is a form on the page, im not sure how to do that, but just have the link go to".....Page.php?click=yes after that in the header of your page go something like :if ($_GET['click] == "yes") {Write the ++1 to the text file. }DISPLAY PAGE HERE.if the link was halfway down the page, i dont know hoew i would save the data already6 saved. ( maybe using the field names and the vairbales and sending all the values, even if their empty.... i doint know)good luck Quote Link to comment Share on other sites More sharing options...
se1zure Posted January 21, 2007 Author Share Posted January 21, 2007 Alright. Well, right now, I just want it to add a click count to the thing, (and it sets a cookie, so that they can't just refresht he page).[code]<?phpif (isset($_COOKIE["user"])){ // if it detects the cookie echo "A hit is only added once per hour"; } // Tell me that it has detected the cookie.else if ($_GET[click] == "yes"){ // If no cookie is detected, and ?click is yes setcookie("user", "VISIT", time()+3600); // Set a cookie $filename= "counter.txt" ; // set filename var to counter.txt file. $fd = fopen ($filename , "r") or die ("Can't open $filename") ; // open counter.txt in read only format. $fstring = fread ($fd , filesize ($filename)) ; //Not %100 sure what this is for, just followed instructions. echo "$fstring" ; // Print counter.txt fclose($fd) ; // Close the connection with counter .txt $fd = fopen ($filename , "w") or die ("Can't open $filename") ; // reopen counter.txt in write format $fcounted = $fstring + 1 ; // Set $fcounted var to add one to $fstring. $fout= fwrite ($fd , $fcounted ) ; // write to $fd connection the new number $fcounted fclose($fd) } // close connection.else { // if neither of the if statments are met. echo "<a href="?click=yes">Visit?</a>"; // display link to visit plage.} ?>[/code]But this code outputs the error:[code]Parse error: syntax error, unexpected '}' in /home2/data/hosted/scetchbook/php/diggtest/story.php on line 15[/code]Which doesn't make sense... I am just closing the if else stament. Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 21, 2007 Share Posted January 21, 2007 Edit: I was wrong. You're missing a ;fclose($fd); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.