Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Web Development
Expertise: Beginner
Sep 8, 1999

Save Inputted Data From a Form as a CSV File

Question:
How do I get all the inputted information from an HTML form and save it to my server as a CSV (comma-separated values) file which updates itself?

Answer:

If you are using Active Server Pages (ASP), this is very easy. The secret to working that magic is in the FileSystemObject. I have included a sample page that you can use as a springboard into your project. Notice that in the sample code, I have included an HTML

. The form, comprising of three HTML boxes and a single Submit button, includes an ASP page as the value of its ACTION property. That ASP page will execute when the user clicks the Submit button. When that ASP page runs, you can extract the values the user entered into the textboxes using this code:

TextFromFirstTextbox = Request.Form("text1")

After you have the values temporarily stored in variables, you can write the values out to your CSV file. The first thing to do is create the FileSystemObject:

Set fs = CreateObject("Scripting.FileSystemObject")

Next, create a text file and hold the reference to that file. The first parameter identifies the path and filename. The second parameter tells the FileSystemObject if it should overwrite the file when an older copy of file exists. I have set the second parameter to "True", which indicates that it should overwrite the file:

	Set a = fs.CreateTextFile(PathAndFileName, True)	

Write the file to disk:

	a.Write(WriteThis)	

Finally, close the file:

	a.Close

That's all there is to it. To see it in action, try this sample code in any browser against a Microsoft IIS 4.0 Web Server.

<%@ Language=VBScript %>
<%
TextFromFirstTextbox = Request.Form("text1")
TextFromSecondTextbox = Request.Form("text2")
TextFromThirdTextbox = Request.Form("text3")

If	Len(TextFromFirstTextbox) > 0 and _
	Len(TextFromSecondTextbox) > 0 and _
	Len(TextFromThirdTextbox) > 0 _
Then
	
	'WRITE TO CSV
	Dim WriteThis
	Dim PathAndFileName 
	
	WriteThis = TextFromFirstTextbox & "," & TextFromSecondTextbox & "," & TextFromThirdTextbox
	PathAndFileName = "C:\MyData.csv"
	
	Set fs = CreateObject("Scripting.FileSystemObject")
	Set a = fs.CreateTextFile(PathAndFileName, True)	
	a.Write(WriteThis)	
	a.Close
	
	Dim MessageOut
	MessageOut =  "The form data (" & WriteThis & ") has been 
written to " & PathAndFileName & ""
	
End If

%>




	

	

	

	
	




    
    


Writing Form Input to a CSV File

<%=MessageOut%>
DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date