PDA

View Full Version : HTML form to file


BarneyRoss
11-03-2010, 12:10 PM
Hi all

I've got a HTML form which goes into a CSV file. However, I want to to have multiple forms that go to different CSV files.

The code I am using is below, I didnt write this (just amended if so it fitted how I needed it!) but I've tried my best to understand as I'm no programmer. I just need to be pointed in the right direction to how I can amend this code so that when form1 is submitted it goes to data1.csv, form 2 goes to data2.csv etc etc.

Many thanks!


<%

for each x in request.form

next


filePath = "F:\data\data1.csv"
set objFSO = server.createobject("Scripting.FileSystemObject")

if (objFSO.fileExists(filePath))=true then
set objTXT = objFSO.openTextFile(filePath, 1) 'opens a text file for
' reading, true means it will create the file if not already there
fullText = trim(objTXT.readall)
lines = split(fullText, vbNewLine) 'lines is now an array, each item is
' one line of the db file.

objTXT.close
set objTXT = nothing

if trim(lines(0)) = "" then fullText = ""
else
fullText = ""
end if %>


<%
if fullText <> "" then 'there are already field names in the db,
' so put the new line in the same order
set objTXT = objFSO.openTextFile(filePath, 8, True) 'opens the text file for
' appending

'split the first line, which had field names into an array -fieldNames-
fieldNames = split(lines(0), ",")

'response.write "field values entered:<br>" & vbNewLine
for each x in fieldNames
if x <> "" then
nLine = nLine & request.form(x) & ","
'adds each form input to a string
end if
next
nLine = left(nLine, len(nLine)-1)
'removes trailing comma

objTXT.writeLine nLine

else 'there isn't anything in the textfile yet, so put in the field names first
set objTXT = objFSO.openTextFile(filePath, 2, True) 'opens the text file for
' writing

for each x in request.form
if lcase(x) <> "submit" then 'or you will have a "submit" field in your db

nLine = nLine & x & ","
'adds each form input name to a string which will become the first line of
'the db, the line which shows field names

response.write x & "<br>" & vbNewLine
end if
next
nLine = left(nLine, len(nLine)-1)
'remove trailing comma

objTXT.write nLine
objTXT.write vbNewLine
nLine = ""

response.write "field values entered:<br>" & vbNewLine
for each x in request.form
if lcase(x) <> "submit" then
nLine = nLine & request.form(x) & ","
'adds each form input to a string

response.write request.form(x) & "<br>" & vbNewLine
end if
next
nLine = left(nLine, len(nLine)-1)
'remove trailing comma

objTXT.write nLine
objTXT.write vbNewLine

end if
objTXT.close
%>

Morgoth
11-03-2010, 06:13 PM
Before you submit your form, you can use three radio buttons to determine if you want the data to go to file1, file2, or file3. Or if each form is on it's own page, you can use a hidden field in the form to identify which form it's being submitted from.

I don't know how your form is set up, but it seems the code you posted walks through each form control submitted. So if you were to use the radio button or hidden form idea, you will need to make two changes.

Form:

<form action="Submit.asp" method="post">
<input type="radio" name="myform" value="form1" /> Form1<br />
<input type="radio" name="myform" value="form2" /> Form2<br />
<input type="radio" name="myform" value="form3" /> Form3<br />
<input type="submit" value="Submit" />
</form>

Or

<form action="Submit.asp" method="post">
<input type="Hidden" name="myform" value="form3" /> Form3 will be submitted
<input type="submit" value="Submit" />
</form>


Form Processing:

<%
If Request.Form("myform") = "form1" Then ' Form1 was submited
FilePath = "F:\data\data1.csv"
ElseIf Request.Form("myform") = "form2" Then ' Form2
FilePath = "F:\data\data2.csv"
Else ' Form3
FilePath = "F:\data\data3.csv"
End If


Set objFSO = Server.CreateObject("Scripting.FileSystemObject")

If (objFSO.FileExists(FilePath)) = True Then
Set objTXT = objFSO.OpenTextFile(FilePath, 1) 'opens a text file for
' reading, true means it will create the file if not already there
FullText = Trim(objTXT.ReadAll)
Lines = Split(FullText, vbNewLine) 'lines is now an array, each item is
' one line of the db file.

objTXT.Close
Set objTXT = Nothing


If Trim(lines(0)) = "" Then
FullText = ""
End If
Else
FullText = ""
End If
%>


<%
If FullText <> "" Then 'there are already field names in the db,
' so put the new line in the same order
Set objTXT = objFSO.OpenTextFile(FilePath, 8, True) 'opens the text file for
' appending

'split the first line, which had field names into an array -fieldNames-
FieldNames = Split(Lines(0), ",")

'response.write "field values entered:<br>" & vbNewLine
For Each x in FieldNames
If x <> "" then
nLine = nLine & Request.Form(x) & ","
'adds each form input to a string
End If
Next
nLine = Left(nLine, Len(nLine) - 1)
'removes trailing comma

objTXT.WriteLine nLine

Else 'there isn't anything in the textfile yet, so put in the field names first
Set objTXT = objFSO.OpenTextFile(FilePath, 2, True) 'opens the text file for
' writing

For Each x In Request.Form
If LCase(x) <> "submit" And LCase(x) <> "form1" And LCase(x) <> "form2" And LCase(x) <> "form3" Then 'or you will have a "submit" field in your db

nLine = nLine & x & ","
'adds each form input name to a string which will become the first line of
'the db, the line which shows field names

Response.Write x & "<br>" & vbNewLine
End If
Next
nLine = Left(nLine, Len(nLine) - 1)
'remove trailing comma

objTXT.Write nLine
objTXT.Write vbNewLine
nLine = ""

Response.Write "Field values entered:<br>" & vbNewLine
For Each x In Request.Form
If LCase(x) <> "submit" Then
nLine = nLine & Request.Form(x) & ","
'adds each form input to a string

Response.Write Request.Form(x) & "<br>" & vbNewLine
End If
Next
nLine = Left(nLine, Len(nLine) - 1)
'remove trailing comma

objTXT.Write nLine
objTXT.Write vbNewLine

End If
objTXT.Close
%>



Let me know if you understand that and if my idea works. After looking at this code, I know there are better ways of going about this, but I would need to see your form code.

BarneyRoss
11-04-2010, 10:13 AM
Thanks Morgoth, that seems to make sense so I'll give this a go today. My form is pretty simple with text boxes and radio buttons - essentially its just:


<form name="form1" action="handler.asp" method="post" id="form1">

<p>Name:</p>
<p><input type="text" name="name" id="name" value="" size="35" </p>

<p>Contact Number:</p>
<p><input type="text" name="name" id="name" value="" size="35" </p>

<input class="formbutton" type="submit" value="Submit"/><input class="formbutton" type="reset" value="Clear" />
</form>


My idea is to have say 5 forms on 5 different pages and each to go to its own CSV file, but using the same handler file.

BarneyRoss
11-04-2010, 02:58 PM
That seems to have worked great, thanks for your help.

In terms of what I'm using/doing though, I am presuming its not the most efficient way? This was the only script I could understand where it could run through the contents of any size form with different types of fields and write back to a CSV.

Morgoth
11-04-2010, 07:50 PM
I am glad it is working for you, just remember to debug the code before you consider it complete.

I also recommend validation of the fields before and/or after you submit your data, you don't want to have a stray "," corrupt your data. You can also validate the contact phone number to make sure it's correct before submitting.