-2

I have spent a couple hours reading up on different methods on how to go about storing data for a submit button counter in a text file.

<form action="/enquiry.php" method="post" name="form">
<label>Name *</label>
<input id="Name" maxlength="100" name="Name" type="text" value="" />
<label>Email Address *</label>
<input id="Email" maxlength="100" name="Email" type="text" value="" />
<button class="btn btn-default" id="submit" name="submit" type="Submit">Submit</button>
</form>

I want to simply have a 'count.txt' file in the main server directory, for which that is displayed on the contact page (Button clicked: X times), every time some one clicks 'Submit' on the contact form.

Can any one recommend the best & safest way to do this?

Thank you.

user3642210
  • 111
  • 1
  • 1
  • 12

1 Answers1

0
index.php
<?php
if(isset($_POST['submit'])){
$file = "count.txt";
$count = file_get_contents($file);
$plus1 = $count + 1;

file_put_contents($file, $plus1);

echo "Button clicked ".":".$plus1." times";


    }
?>

<form action="" method="post" name="form">
<label>Name *</label>
<input id="Name" maxlength="100" name="Name" type="text" value="" />
<label>Email Address *</label>
<input id="Email" maxlength="100" name="Email" type="text" value="" />
<button class="btn btn-default" id="submit" name="submit" type="Submit">Submit</button>
</form>

This is simple example in which i write all my code is index.php and my count.txt file is in same folder where index.php. Please note if you use action= "/enquiry.php" then use my php coding in enquiry.php. Also please change file path according to your file path.

Passionate Coder
  • 7,154
  • 2
  • 19
  • 44
  • Thanks for this... but the enquiry.php file is directed to a /thank-you.htm after the form has run. How would I echo the code on the /thank-you.htm page? Thanks :) – user3642210 Jun 23 '16 at 17:18
  • Never mind... had a play about and sussed it. Thank you very much :) – user3642210 Jun 23 '16 at 17:24