Search code examples
vb.netasp-classictextareawritetofile

ASP Writing Values from Textbox to Textfile


I have a textarea (id=output2) that has a list of numbers in it. I have ASP code that writes data to a text file and it works if I specify the text.

Code to Write to Text File:

<%
function WriteToFile(FileName, Contents, Append)
on error resume next

if Append = true then
   iMode = 8
else 
   iMode = 2
end if
set oFs = server.createobject("Scripting.FileSystemObject")
set oTextFile = oFs.OpenTextFile(FileName, iMode, True)
oTextFile.Write Contents
oTextFile.Close
set oTextFile = nothing
set oFS = nothing

end function

%>

Then this code overwrites the file and inserts the data AND WORKS:

<%
WriteToFile "C:\INSTALL\Test1.txt", "Why is this so difficult??", False
%>

But yet if I leave the first code alone and try to get dynamic data such as from a textarea field it fails with no error. Doesn't even touch the text file. I tried many many ways... here are five that FAILED:

1:

<%
WriteToFile "C:\INSTALL\Test1.txt", Document.getElementById("output2"), False
%>

2:

<%
dim texttoinsert
texttoinsert = Document.getElementById("output2")
WriteToFile "C:\INSTALL\Test1.txt", texttoinsert, False
%>

3:

<%
WriteToFile "C:\INSTALL\Test1.txt", Response.Write(Document.getElementById("output2")), False
%>

4:

<%
dim texttoinsert
texttoinsert = "Starting to hate this"
WriteToFile "C:\INSTALL\Test1.txt", texttoinsert, False
%>

5:

<%
dim texttoinsert
texttoinsert = "Definitely hate this"
WriteToFile "C:\INSTALL\Test1.txt", Response.Write(texttoinsert), False
%>

I even did some VBscript that was able to tap into the Document.getElementById but I couldn't figure out how to get it to go into the ASP code.


Solution

  • document.getElementByID doesn't have any meaning in server-side code. You have to submit a form to pass data to function page and then use it like this:

    html page:

    <form action="myfunction.asp">
    <input name="texttoinsert">
    <input type="submit" value="write data">
    </form>
    

    myfunction.asp

    <%
    function WriteToFile(FileName, Contents, Append)
     on error resume next
     if Append = "true" then
        iMode = 8
     else 
        iMode = 2
     end if
     set oFs = server.createobject("Scripting.FileSystemObject")
     set oTextFile = oFs.OpenTextFile(FileName, iMode, True)
     oTextFile.Write Contents
     oTextFile.Close
     set oTextFile = nothing
     set oFS = nothing
    end function
    
    texttoinsert = request("texttoinsert")
    WriteToFile "C:\INSTALL\Test1.txt", texttoinsert, False
    %>