Request  info and save it to a file  (.asp) 

In this example we will use a form to request information to our visitors and we will saved that information to a file. As we may get information from different visitors, in this example we will create a new file each time we get info from a new visitor. The script will create all necessary folders and files automatically. This script has been generated specifically for you (in your next visit the code will be prepared to save the information from your visitors in a different place within your server)
  • All the information obtained from our form will be saved in  a subdirectory within cgi-bin folder (p.e.: "c:\mydir\cgi-bin\messages143718\"), and the information will be saved in a ".txt" file (p.e.: "c:\mydir\cgi-bin\messages143718\1.txt"). That way we will prevent other users form getting information without permission (a ".txt" file located in the cgi-bin directory is not accessible by the client). The subdirectory necessary to save the information will be create automatically.
  • We will also need a file with a number as its unique content .This file will be used as a counter, and each time a visitor fills in our form the information provided will be save to a file (for example information from the first visitor will be save in  file  "1.txt", information from the second one in "2.txt"...). The counter file will be create automatically. 
This script will allow us to add any kind and number of input fields within the form, so that we may use the same response page for all forms in our site ("saveinfo.asp" may be used to save information from "form1.html", "form2.html" etc.)
 
 
form.html     Example code
<FORM ACTION="saveinfo.asp" METHOD=post>

<!-- Your fields here -->

<INPUT TYPE=submit value="Submit">
</FORM>


 
 
saveinfo.asp
<%
Set fs = CreateObject("Scripting.FileSystemObject")

Folderpath=server.mappath("\") & "/cgi-bin/messages143718" 
Wcounter=Folderpath &"/counter.txt" 

Set fs = CreateObject("Scripting.FileSystemObject") 
    if fs.FolderExists(Folderpath) then 
        Set a = fs.OpenTextFile(Wcounter) 
        hits = Clng(a.ReadLine) 
        hits = hits 1 
        a.close 
    else 
        Set a = fs.CreateFolder(Folderpath) 
        hits=1
    end if 

Set a = fs.CreateTextFile(Wcounter,True) 
a.WriteLine(hits) 
a.Close
Set fs=nothing 

Set fs = CreateObject("Scripting.FileSystemObject") 
Set a = fs.CreateTextFile(Folderpath & "\" & hits & ".txt") 
   For Each x In Request.Form
      a.WriteLine(x &": " & Request.Form(x))
   Next
a.Close 
Set a=nothing 
Set fs=nothing 
%> 

Thanks for providing the requested information<BR>
I will contact you very soon. 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
  • Lines 2-18. In case the folder where information will be saved does not exits, it will be create.  The counter file will be also create. If they already exits, the counter will be open, the content read to a variable named "hits", and increased by one. 
  • Lines 20-23. the value for variable h"hits" will be save in "counter.txt"  file in our folder. 
  • Lines 25-32, information provided  per input field will be saved to a file. The name of the file will be composed by the variable hits (a number) and ".txt"
  • Lines 34-35. Write here your response. 
    • The response may be a text, as in this example, 
    • You may redirect the visitor to a different page. The code will be this one:

    • <% response.redirect("http://www.mysite.com") %>
    • You may include as a response page the content in a page within your site

    •  <!--#include virtual="/myresponse.html" --> 
I
In order to access the information saved to files we may use the script bellow. That way we will be able to access the information from all visitors in an unique page.  Access to information is password protected (Name and password to access the information are shown in line two. If the name and password are not correct the form will be send (Subroutine "Sendform ()"), and in case they are correct the info is send (Subroutine "Sendinfo ()"). 
 
getinfo.asp  Try !
<% 
if request.form("name")="Peter" and request.form("password")="yes" then 
Sendinfo ()
else 
Sendform ()
end if 
%>

<% 
Sub Sendform() %> 

<FORM ACTION="getinfo.asp" METHOD=post>
Name: <input type=text name=name><br>
Password: <input type=password name=password><br>
<INPUT TYPE=submit value="Submit">
</FORM>

<% End Sub %>
 

<% 
Sub Sendinfo()

FolderToCheck = server.mappath("\") & "/cgi-bin/messages143718"

 Dim fs, f, f1, fc, s
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFolder(FolderToCheck)
 Set fc = f.Files
 

For Each f1 in fc
  Wfile=f1.name

Response.write("<hr><b>" & Wfile & "/" & "</b><p>")
   FiletoCheck=FolderToCheck  & Wfile
    Set a=fs.OpenTextFile(FiletoCheck)
     theinfo=a.ReadAll
Response.write("<pre>" & Theinfo & "</pre>")
Next
Response.write("<hr>Script provided by <a href=http://www.asptutorial.info>asptutorial.info</a>")

End sub
%>

 

AspTutorial.info