Solved

Dumping form to CSV

Posted on 2003-11-29
18
1,870 Views
Last Modified: 2012-08-13
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
Comment
Question by:coreybryant
  • 9
  • 8
18 Comments
 
LVL 33

Expert Comment

by:hongjun
ID: 9844215
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
 
LVL 33

Expert Comment

by:hongjun
ID: 9844217
note that above open file in append mode.
0
 
LVL 33

Accepted Solution

by:
hongjun earned 500 total points
ID: 9844293
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
 
LVL 29

Author Comment

by:coreybryant
ID: 9844321
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
 
LVL 29

Author Comment

by:coreybryant
ID: 9844343
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
 
LVL 33

Expert Comment

by:hongjun
ID: 9844624
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
 
LVL 33

Expert Comment

by:hongjun
ID: 9844629
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
 
LVL 29

Author Comment

by:coreybryant
ID: 9844794
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
 
LVL 29

Author Comment

by:coreybryant
ID: 9844894
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 33

Expert Comment

by:hongjun
ID: 9844979
0
 
LVL 29

Author Comment

by:coreybryant
ID: 9845231
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
 
LVL 29

Author Comment

by:coreybryant
ID: 9845241
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
 
LVL 33

Expert Comment

by:hongjun
ID: 9845307
>>Now is there a way not to have the input names added each time
Sorry don't quite understand you.

hongjun
0
 
LVL 29

Author Comment

by:coreybryant
ID: 9845367
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
 
LVL 33

Expert Comment

by:hongjun
ID: 9845393
It can be this only

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

hongjun
0
 
LVL 29

Author Comment

by:coreybryant
ID: 9845401
That's what I thought.  But wanted to verify.  Thanks!

-Corey
0
 
LVL 33

Expert Comment

by:hongjun
ID: 9845402
With that you need a counter to do a vbcrlf.
0
 

Expert Comment

by:mrroy69
ID: 33642987
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now