devxlogo

Save Inputted Data From a Form as a CSV File

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.

 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

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist