Link to home
Start Free TrialLog in
Avatar of coreybryant
coreybryantFlag for United States of America

asked on

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
Avatar of hongjun
hongjun
Flag of Singapore image

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
note that above open file in append mode.
ASKER CERTIFIED SOLUTION
Avatar of hongjun
hongjun
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of coreybryant

ASKER

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
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
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
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
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
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
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
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
>>Now is there a way not to have the input names added each time
Sorry don't quite understand you.

hongjun
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
It can be this only

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

hongjun
That's what I thought.  But wanted to verify.  Thanks!

-Corey
With that you need a counter to do a vbcrlf.
Avatar of mrroy69
mrroy69

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"
%>