• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1921
  • Last Modified:

Dumping form to CSV

I am pretty sure it is possible, but where can I get some step by step plays on doing this for someone that knows nothing about ASP.  I told someone I would help them with a form.  If you could provide me with the start of the code and say one input (name)  and the end of the code- I should be able add all the other inputs, right (hopefully).

Thanks!

-Corey
0
coreybryant
Asked:
coreybryant
  • 9
  • 8
1 Solution
 
hongjunCommented:
Try below
request("realname") and request("email") are the 2 form fields passed in.


<%
dim strFile, strtext
dim a

strFile="myfile"
Set fs = Server.CreateObject("Scripting.FileSystemObject")
Set a = fs.OpenTextFile(server.MapPath(".\") & strFile & ".csv", 8, true)

'this create the file called report.csv
strtext=""
strtext = strtext & chr(34) & "Name" & chr(34) & ","
strtext = strtext & chr(34) & "Email Address" & chr(34) & ","
a.WriteLine(strtext)
'this create two columns in excel file

strtext =""
strtext = strtext & chr(34) & request("realname") & chr(34) & ","
strtext = strtext & chr(34) & request("email") & chr(34) & ","
a.Writeline(strtext)
'this will read the content fm a form and write into excel
%>


hongjun
0
 
hongjunCommented:
note that above open file in append mode.
0
 
hongjunCommented:
i think this is closer to what you wanted.
NO hard coding of form fields.


<%
dim strFile, strtext
dim a

strFile="\myfile"
Set fs = Server.CreateObject("Scripting.FileSystemObject")
Set a = fs.OpenTextFile(server.MapPath("\") & strFile & ".csv", 8, true)

'this create the file called report.csv
strtext=""

for each str in Request.Form
      strtext = strtext & chr(34) & str & chr(34) & ","
next

a.WriteLine(strtext)
strtext =""

for each str in Request.Form
      strtext = strtext & chr(34) & Request.Form(str) & chr(34) & ","
next

a.Writeline(strtext)
'this will read the content fm a form and write into excel
%>


hongjun
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
coreybryantAuthor Commented:
Thanks hongjun - just a couple of questions whiel I am working thru this.   When you say this create the file called report.csv, do I need to create this file or will it do it automatically?  Also, not too sure what you mean by hard coding of form fields?  Do you mean that it does not matter how many form fields that I have & I do not have to worry about creating the CSV file?

Thanks!

-Corey
0
 
coreybryantAuthor Commented:
Lets assume, on my contact form (right now):
<html>

<head>
<title>Contact</title>
</head>

<body>

<form method="Post" action="confirm.asp">
  <input type="text" name="Name" size="20"></p>
  <p><input type="text" name="EMail" size="20"></p>
  <p><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>
</form>

</body>

</html>
 And then on the confirm.asp:
<%
dim strFile, strtext
dim a

strFile="\file"
Set fs = Server.CreateObject("Scripting.FileSystemObject")
Set a = fs.OpenTextFile(server.MapPath("\") & strFile & "report.csv", 8, true)

'this create the file called report.csv
strtext=""

for each str in Request.Form
    strtext = strtext & chr(34) & str & chr(34) & ","
next

a.WriteLine(strtext)
strtext =""

for each str in Request.Form
    strtext = strtext & chr(34) & Request.Form(str) & chr(34) & ","
next

a.Writeline(strtext)
'this will read the content fm a form and write into excel
%>

And then I have a report.csv in the file folder (one down).  Do I need to dim the form inputs?

Thanks!

-Corey
0
 
hongjunCommented:
Yap using my second code, you need not create the .csv file. You also need not worry about the number of form fields you are going to submit because of the for loop I am using.

hongjun
0
 
hongjunCommented:
you need not dim the form inputs. Only dim the str
dim str

But if you are not using Option Explicit, you need not dim anything.

hongjun
0
 
coreybryantAuthor Commented:
Sounds cool.  I will be needing to dim the inputs for my JMail script, but I  am having a problem....  I am using :
<form method="Post" action="confirm.asp">
  <input type="text" name="Name" size="20"></p>
  <p><input type="text" name="EMail" size="20"></p>
  <p><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>
</form>

and then in the confirm.asp:
<%
dim strFile, strtext
dim a

strFile="\file"
Set fs = Server.CreateObject("Scripting.FileSystemObject")
Set a = fs.OpenTextFile(server.MapPath("\") & strFile & ".csv", 8, true)

'this create the file called report.csv
strtext=""

for each str in Request.Form
    strtext = strtext & chr(34) & str & chr(34) & ","
next

a.WriteLine(strtext)
strtext =""

for each str in Request.Form
    strtext = strtext & chr(34) & Request.Form(str) & chr(34) & ","
next

a.Writeline(strtext)
'this will read the content fm a form and write into excel
%>

and the architecture is something like this:
-www(folder)
 default.asp (file)
 confirm.asp (file)
--file (folder)

And I am getting this error:
Permission denied

/sites/auto/confirm.asp, line 7

I do have it checked for the read/write permissions to accept anonymous write.

Thanks!

-Corey
0
 
coreybryantAuthor Commented:
I also have my inputs if that will help now, so maybe I can create a reference?

<%
Dim Last_Namename,Last_Name,First_Namename,First_Name,Email_Addressname,Email_Address
Dim Home_Phonename,Home_Phone,Work_Phonename,Work_Phone,Extname,Ext,Cell_Phonename,Cell_Phone
Dim Addressname,Address,Cityname,City,Statename,State,ZipName,Zip
Dim Makename,Make,Modelname,Model,yearname,year,mileagename,mileage,purchase_yearname,purchase_year
Dim New_Usedname,New_Used,Purchase_pricename,Purchase_price,Four_Wheel_Drivename,Four_Wheel_Drive,Turboname,Turbo
Dim Engine_Typename,Engine_Type,Cylindersname,Cylinders,Best_Time_to_Contact_Youname,Best_Time_to_Contact_You,Warrantyname,Warranty
Dim IPname,IP,User_Agentname,User_Agent
Dim strRefername,strRefer
Last_Namename = "Last Name: "
Last_Name = Request.Form("Last_Name")
First_Namename = "First Name: "
First_Name = Request.Form("First_Name")
Home_Phonename = "Home Phone: "
Home_Phone = Request.Form("Home_Phone")
Work_Phonename = "Work Phone: "
Work_Phone = Request.Form("Work_Phone")
Extname = "Extension: "
Ext = Request.Form("Ext")
Cell_Phonename = "Cell Phone: "
Cell_Phone = Request.Form("Cell_Phone")
Email_Addressname = "E-Mail Address: "
Email_Address = Request.Form("Email_Address")
Addressname = "Address: "
Address = Request.Form("Address")
Cityname = "City: "
City = Request.Form("City")
Statename = "State: "
State = Request.Form("State")
Zipname = "Zip: "
Zip = Request.Form("Zip")
Makename = "Make: "
Make = Request.Form("Make")
Modelname = "Model: "
Model = Request.Form("Model")
yearname = "Year: "
year = Request.Form("year")
mileagename = "Mileage: "
mileage = Request.Form("mileage")
purchase_yearname = "purchase_year: "
purchase_year = Request.Form("Purchase Year")
New_Usedname = "New or Used: "
New_Used = Request.Form("New_Used")
Purchase_pricename = "Purchase Price: "
Purchase_price = Request.Form("Purchase_price")
Four_Wheel_Drivename = "Four Wheel Drive: "
Four_Wheel_Drive = Request.Form("Four_Wheel_Drive")
Turboname = "Turbo: "
Turbo = Request.Form("Turbo")
Engine_Typename = "Engine Type: "
Engine_Type = Request.Form("Engine_Type")
Cylindersname = "Cylinders: "
Cylinders = Request.Form("Cylinders")
Best_Time_to_Contact_Youname = "Best Time to Call: "
Best_Time_to_Contact_You = Request.Form("Best_Time_to_Contact_You")
Warrantyname = "Warranty: "
Warranty = Request.Form("Warranty")
IPname = "IP Address: "
IP = Request.Form("IP")
User_Agentname = "Browser Info: "
User_Agent = Request.Form("User_Agent")
strRefername = "URL: "
strRefer = Request.Form("strRefer")
Set JMail = Server.CreateObject("JMail.Message")
JMail.MailServerUserName = "user@mydomain.com"
JMail.MailServerPassword = "password"
JMail.ISOEncodeHeaders = False
JMail.From = "contact@mydomain.com"
JMail.AddRecipient "contact@mydomain.com"
JMail.Subject = "Contact from mydomain"
JMail.Body = Last_Namename & Last_Name & vbcrlf&_
First_Namename & First_Name & vbcrlf&_
Home_Phonename & Home_Phone & vbcrlf&_
Work_Phonename & Work_Phone & vbcrlf&_
Extname & Ext & vbcrlf&_
Cell_Phonename & Cell_Phone & vbcrlf&_
Email_Addressname & Email_Address & vbcrlf&_
Addressname & Address & vbcrlf&_
Cityname & City & vbcrlf&_
Statename & State & vbcrlf&_
Zipname & Zip & vbcrlf&_
Makename & Make & vbcrlf&_
Modelname & Model & vbcrlf&_
yearname & year & vbcrlf&_
mileagename & mileage & vbcrlf&_
purchase_yearname & purchase_year & vbcrlf&_
New_Usedname & New_Used & vbcrlf&_
Purchase_pricename & Purchase_price & vbcrlf&_
Four_Wheel_Drivename & Four_Wheel_Drive & vbcrlf&_
Turboname & Turbo & vbcrlf&_
Engine_Typename & Engine_Type & vbcrlf&_
Cylindersname & Cylinders & vbcrlf&_
Best_Time_to_Contact_Youname & Best_Time_to_Contact_You & vbcrlf&_
Warrantyname & Warranty & vbcrlf&_
vbcrlf&_
IPname & IP & vbcrlf&_
User_Agentname & User_Agent & vbcrlf&_
strRefername & strRefer
JMail.Priority = 1
JMail.Send("mail.mydomain.com")
%>

I take it tha dumping it to a CSV, you do not have to worry about mapping?  But I want to make sure that if a field is added in the future (unlikely) that it would not mess anything up.  The inputs (by themselves):
First_Name
Last_Name
Home_Phone
Work_Phone
Cell_Phone
Ext
Email_Address
Address
Apt
City
State
Zip
Make
Model
year
mileage
purchase_year
New_Used
Purchase_price
Four_Wheel_Drive
Turbo
Engine_Type
Cylinders
Best_Time_to_Contact_You
Warranty

Thanks!

-Corey
0
 
hongjunCommented:
0
 
coreybryantAuthor Commented:
Well I did sorta figure part of it out, but I changed the code to
strFile="\file"
Set fs = Server.CreateObject("Scripting.FileSystemObject")
Set a = fs.OpenTextFile(server.MapPath("\file") & strFile & ".csv", 8, true)

and I get:
Path not found

/sites/auto/confirm.asp, line 7

Getting closer I think.

Thanks!

-Corey
0
 
coreybryantAuthor Commented:
This seemed to work:
strFile="\file"
Set fs = Server.CreateObject("Scripting.FileSystemObject")
Set a = fs.OpenTextFile(server.MapPath("file") & strFile & ".csv", 8, true)


Now is there a way not to have the input names added each time?  If not, that's cool.

Thanks!

-Corey
0
 
hongjunCommented:
>>Now is there a way not to have the input names added each time
Sorry don't quite understand you.

hongjun
0
 
coreybryantAuthor Commented:
Well right now the text is:
Name      EMail      B1
Corey Bryant      crb@merchantaccount4less.com      Submit
Name      EMail      B1
Corey Bryant      crb@merchantaccount4less.com      Submit

Can it just be:
Corey Bryant      crb@merchantaccount4less.com      Submit
Corey Bryant      crb@merchantaccount4less.com      Submit

-Corey
0
 
hongjunCommented:
It can be this only

Name EMail       B1
Corey Bryant     crb@merchantaccount4less.com
Corey Bryant     crb@merchantaccount4less.com
Submit

hongjun
0
 
coreybryantAuthor Commented:
That's what I thought.  But wanted to verify.  Thanks!

-Corey
0
 
hongjunCommented:
With that you need a counter to do a vbcrlf.
0
 
mrroy69Commented:
Yes you can only have

Corey Bryant     crb@merchantaccount4less.com
Corey Bryant     crb@merchantaccount4less.com

Just delete the first part of the loop and only use the second part like this:

<%
dim strFile, strtext
dim a

strFile="\members"
Set fs = Server.CreateObject("Scripting.FileSystemObject")
Set a = fs.OpenTextFile(server.MapPath(".\") & strFile & ".csv", 8, true)

'this create the file called members.csv
strtext =""
strtext = strtext & chr(34) & request("accountno") & chr(34) & ","
strtext = strtext & chr(34) & request("firstname") & chr(34) & ","
strtext = strtext & chr(34) & request("lastname") & chr(34) & ","
strtext = strtext & chr(34) & request("email") & chr(34) & ","
a.Writeline(strtext)
'this will read the content fm a form and write into excel
%>
<%
response.redirect "signup.asp"
%>
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 9
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now