user3642210 user3642210 - 5 months ago 22
jQuery Question

form post submit button counter to store data in .txt

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.

Answer
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.

Comments