Hi! This is a simple databaseless script to save form data to a file. The code is split into four parts:
- The HTML form
- The PHP code
- The data file
- The .htaccess file
Your document should contain a similar form:
<form novalidate="" method="post" action="submit.php" id="SchoolDetails">
<label for="School">School School:</label>
<input id="School" name="School" type="text" placeholder="School" value="" spellcheck="false" class="">
<label for="TeamSchool">Team Name:</label>
<input id="Team" name="Team" type="text" placeholder="Team" value="" spellcheck="false" class="">
<input id="submit" name="submit" type="submit" value="Submit!">
Every Input must have the 'name' attribute. Remember to change
submit.php according to the name of your PHP file
This is the PHP code that does the actual work:
header("Location: thanks.html"); //This is the page to which the user will be redirected...
$handle = fopen("userdata.txt", "a"); //this is the name of the text file in which all the data will get stored...
$post = $_POST;
$post['----------=User Information'] = '----------';
$post['User IP'] = $_SERVER['REMOTE_ADDR']; //Saves the IP Address
$post['Browser/UserAgent'] = $_SERVER['HTTP_USER_AGENT']; //Saves the User Agent
$post['Referrer'] = $_SERVER['HTTP_REFERER']; //Saves the URL of the page which contains the form
$post['HTTPS'] = $_SERVER['HTTPS']; //Is the script queried through a secure HTTP protocol?
$post['Date&Time'] = date("l jS \of F Y h:i:s A"); //Saves the date and time at which the form was submitted
//add more global variables here
$post['========================END'] = '=======================';
//Save all the form data as Input_Name=Input_Value
foreach($post as $variable => $value)
You can add many more PHP global variables. Change
thanks.html to point to the webpage to which the users are redirected once they submit the form.
userdata.txt is the file to which everything is stored. You can name it anything, but be sure to change the file name everywhere in the code.
The data file:
Just create an empty file called
userdata.txt or whatever you want to call it and everything will be saved to it.
Note: It doesn't have to be a
.txt file, it can be any format, just make sure it isn't
.php because that can be a big security vulnerability.
The .htaccess file:
Now, you have to make a decision. Do you want to be able to see the result on the go without logging in every time? If so, rename your data file to something guessable like
MyUserData5566789334.txt so that only you know the URL. After this, just visit the file in your web browser to see the entries. However, if you want something more secure, edit the .htaccess file in the directory in which your file is stored to contain the following:
Deny from all
The downside of this is that you will have to use FTP or your server's web-based administration system to view the file every time.
That's it! Enjoy your new submission form!