INSERT DATA FROM MULTIPLE TEXTAREAS IN THE FORM TO THE DATABASE

Discussion in 'ASP General' started by BLUESTAR, Jul 10, 2008.

  1. BLUESTAR

    BLUESTAR Guest

    HELLO,

    I AM A NEW BII TO ASP,AND I NEED HELP ON THIS ISSUE.
    I HAVE A .ASP FORM WHICH IS ALREADY BUILT WITH 5 QUESTIONS AND 5 TEXTAREAS
    RESPECTIVELY.THESE 5 QUESTIONS ARE NOT FIX,THERE WILL BE MORE ADDITION.

    ITS BASICALLY A SURVEY FORM.

    I HAVE CREATED A TABLE WITH 3 FIELDS :QUESTION ID,RESPONSE IN TEXTAREA, AND
    DATE.
    AND THESE 3 ARE TO BE INSERTED IN DATABASE VIA THIS FORM.

    I HAVE TO 1ST COUNT /PARSE HOW MANY TEXTAREAS ARE THERE IN THIS FORM,THEN
    EXTRACT EACH TEXTAREA ID.

    AND THEN FINALLY I HAVE TO INSERT QESTION ID,TEXTAREA RESPONSE TO THE DATABSE.

    I WILL BE REALLY GRATEFUL IF SOMEBODY CAN HELP ME TO PROCEED THROUGH IT.

    I HAVE BEEN ADVICE TO USE SOME FUNCTIONS TO IMPLEMENT THIS

    HERE IS THE LINK OF THIS FORM
    http://localhost/testasp/SurveyResponse.asp

    THANK YOU

    WILL BE EGARLY WAITING FOR SOME REPLY
    BLUESTAR, Jul 10, 2008
    #1
    1. Advertising

  2. BLUESTAR

    Old Pedant Guest

    Easiest way is to just number the fields.

    Example:

    <FORM Action="processAnswers.asp" Method=POST>
    Question: How much is 3 + 7 * 10 ?
    Answer: <TEXTAREA Name="answer1"></TEXTAREA>
    Question: What are your favorite foods?
    Answer: <TEXTAREA Name="answer2"></TEXTAREA>
    Question: Describe the effects of moonlight on the flight of Monarch
    butterflies:
    Answer: <TEXTAREA Name="answer3"></TEXTARE>
    ....
    <INPUT Type=Submit Value="Submit my answers">
    </FORM>

    And then you "processAnswers.asp" page simply does this:

    <%
    Set conn = Server.CreateObject("ADODB.Connection")
    conn.Open "...your connection string ..."

    For answerNumber = 1 TO 3
    answer = Request("answer" & answerNumber)
    If answer <> "" Then
    SQL = "INSERT INTO answers( user, answerNumber, answer ) " _
    & " VALUES(" & userid & "," & answerNumber & "," _
    & "'" & Replace(answer, "'", "''") & "')"
    conn.Execute SQL
    End If
    Next
    conn.Close
    %>
    Old Pedant, Jul 10, 2008
    #2
    1. Advertising

  3. BLUESTAR

    BLUESTAR Guest

    RE: INSERT DATA FROM MULTIPLE TEXTAREAS IN THE FORM TO THE DATABAS

    Well first of all thank a ton for reply.
    Here is the link of the page where I have to perform insertion of data in
    the databse.http://www.smartcharlotte2050.com/YourThoughts.asp

    I have been told to write some function that will traverse through the form
    and will count textareas and also search for each textareaID and insert it
    in the question ID column of the table.

    Is there any way ,on how to traverse through the form.asp source code and
    count textareas and insert its content in database.

    Thanks again.
    And again expecting a quick response.




    "Old Pedant" wrote:

    > Easiest way is to just number the fields.
    >
    > Example:
    >
    > <FORM Action="processAnswers.asp" Method=POST>
    > Question: How much is 3 + 7 * 10 ?
    > Answer: <TEXTAREA Name="answer1"></TEXTAREA>
    > Question: What are your favorite foods?
    > Answer: <TEXTAREA Name="answer2"></TEXTAREA>
    > Question: Describe the effects of moonlight on the flight of Monarch
    > butterflies:
    > Answer: <TEXTAREA Name="answer3"></TEXTARE>
    > ...
    > <INPUT Type=Submit Value="Submit my answers">
    > </FORM>
    >
    > And then you "processAnswers.asp" page simply does this:
    >
    > <%
    > Set conn = Server.CreateObject("ADODB.Connection")
    > conn.Open "...your connection string ..."
    >
    > For answerNumber = 1 TO 3
    > answer = Request("answer" & answerNumber)
    > If answer <> "" Then
    > SQL = "INSERT INTO answers( user, answerNumber, answer ) " _
    > & " VALUES(" & userid & "," & answerNumber & "," _
    > & "'" & Replace(answer, "'", "''") & "')"
    > conn.Execute SQL
    > End If
    > Next
    > conn.Close
    > %>
    >
    BLUESTAR, Jul 10, 2008
    #3
  4. BLUESTAR

    Old Pedant Guest

    RE: INSERT DATA FROM MULTIPLE TEXTAREAS IN THE FORM TO THE DATABAS

    > Is there any way ,on how to traverse through the form.asp source code and
    > count textareas and insert its content in database.


    Yes. Use the code I gave you.

    What did you THINK it was doing????

    It's doing *EXACTLY* that.

    But forget ID's on the fields. IDs don't matter in ASP code. Use names, as
    I showed you.

    Did you even TRY that code???
    Old Pedant, Jul 11, 2008
    #4
  5. BLUESTAR

    Old Pedant Guest

    RE: INSERT DATA FROM MULTIPLE TEXTAREAS IN THE FORM TO THE DATABAS

    Using the FORM you actually show there, you would just alter my code to

    <%
    Set conn = Server.CreateObject("ADODB.Connection")
    conn.Open "...your connection string ..."

    For answerNumber = 1 TO 4
    answer = Request("Your Thoughts Q" & answerNumber)
    If answer <> "" Then
    SQL = "INSERT INTO answers( answerNumber, answer ) " _
    & " VALUES(" & answerNumber & "," _
    & "'" & Replace(answer, "'", "''") & "')"
    conn.Execute SQL
    End If
    Next
    conn.Close
    %>

    You didn't bother to show us the schema of your DB table, so I just assumed
    TABLE: answers
    answerNumber int
    answer text
    Old Pedant, Jul 11, 2008
    #5
  6. Re: INSERT DATA FROM MULTIPLE TEXTAREAS IN THE FORM TO THE DATABAS

    Old Pedant wrote:
    >> Is there any way ,on how to traverse through the form.asp source
    >> code and count textareas and insert its content in database.

    >
    > Yes. Use the code I gave you.
    >
    > What did you THINK it was doing????
    >
    > It's doing *EXACTLY* that.
    >
    > But forget ID's on the fields. IDs don't matter in ASP code. Use
    > names, as I showed you.
    >
    > Did you even TRY that code???

    Cmon, pedant, hop to it. He's expecting a quick response. Stop dithering now
    .... :)

    --
    Microsoft MVP - ASP/ASP.NET
    Please reply to the newsgroup. This email account is my spam trap so I
    don't check it very often. If you must reply off-line, then remove the
    "NO SPAM"
    Bob Barrows [MVP], Jul 11, 2008
    #6
  7. BLUESTAR

    Old Pedant Guest

    Re: INSERT DATA FROM MULTIPLE TEXTAREAS IN THE FORM TO THE DATABAS

    "Bob Barrows [MVP]" wrote:
    > Cmon, pedant, hop to it. He's expecting a quick response. Stop dithering now
    > .... :)


    No fair! Making the people around me give me strange looks for ROTFLMAO.

    Too funny! Needed that!
    Old Pedant, Jul 11, 2008
    #7
  8. BLUESTAR

    BLUESTAR Guest

    RE: INSERT DATA FROM MULTIPLE TEXTAREAS IN THE FORM TO THE DATABAS

    Thank You
    And also,sorry for thanking you late,

    BUT I am getting error in the insert statement, and i am not being able to
    solve it.

    2ndly in this form,later more questions will be added, so is there any
    simple way to count the # of textareas and then use it in for loop.

    After my long search on google I found one thing...there is a class define

    Class UtilityObj
    Private _name As String
    Private _value As String
    Public Sub New(ByVal Name As String,ByVal Value As int)
    _name = Name
    _value = Value
    End Sub
    Public Property Name() As String
    Get
    Return _name
    End Get
    Set(ByVal Value As String)
    _name = Name
    End Set
    End Property
    Public Property Value() As String
    Get
    Return (_value)
    End Get
    Set(ByVal Value As String)
    _value = Value
    End Set
    End Property
    End Class


    then using this function the name and ID is being inserted in the database



    Funtion LoopingControls(oControl As Control)
    Dim frmCtrl As Control
    oArrayList = New ArrayList
    For each frmCtrl in oControl.Controls
    If TypeOf frmCtrl Is TextArea Then

    Call FunctionDB(db)

    End If
    If frmCtrl.HasControls Then
    LoopingControls(frmCtrl)
    End If
    Next
    End Function


    This is the function which will do database connection.

    Function FunctionDB(db)

    Set db=Server.CreateObject ("ADODB.Connection")

    db.Open "DSN=surveydsn;User ID=dbo_sage;Password=sage;"

    SQL = "INSERT INTO survey ( i, res ) "
    & " VALUES(" & i & ","
    & "'" & Replace(res, "'", "''") & "')"
    db.Execute SQL
    db.Close
    db.Execute(VarQuery)
    End Function



    Now the thing is I am messed up with this, I have all the logic to
    perform,but not being able to assemble it.


    Lastly somebody told me to use Microsoft Visual studio 6, well i am not
    being able to open interdev as I dont have FrontPage 98 server extension and
    also Its not available on net free to download,


    Hope you will again help me.

    Thank again for your support,it really helped me.

    "Old Pedant" wrote:

    > Using the FORM you actually show there, you would just alter my code to
    >
    > <%
    > Set conn = Server.CreateObject("ADODB.Connection")
    > conn.Open "...your connection string ..."
    >
    > For answerNumber = 1 TO 4
    > answer = Request("Your Thoughts Q" & answerNumber)
    > If answer <> "" Then
    > SQL = "INSERT INTO answers( answerNumber, answer ) " _
    > & " VALUES(" & answerNumber & "," _
    > & "'" & Replace(answer, "'", "''") & "')"
    > conn.Execute SQL
    > End If
    > Next
    > conn.Close
    > %>
    >
    > You didn't bother to show us the schema of your DB table, so I just assumed
    > TABLE: answers
    > answerNumber int
    > answer text
    >
    >
    BLUESTAR, Jul 14, 2008
    #8
  9. BLUESTAR

    BLUESTAR Guest

    RE: INSERT DATA FROM MULTIPLE TEXTAREAS IN THE FORM TO THE DATABAS

    I forgot to say that,more questions will be added,but I dont know how
    many,so i need to traverse the form in order to count # of textareas, so the
    for loop from 1 to 4 will not work in that case

    Thank You
    "BLUESTAR" wrote:

    > Thank You
    > And also,sorry for thanking you late,
    >
    > BUT I am getting error in the insert statement, and i am not being able to
    > solve it.
    >
    > 2ndly in this form,later more questions will be added, so is there any
    > simple way to count the # of textareas and then use it in for loop.
    >
    > After my long search on google I found one thing...there is a class define
    >
    > Class UtilityObj
    > Private _name As String
    > Private _value As String
    > Public Sub New(ByVal Name As String,ByVal Value As int)
    > _name = Name
    > _value = Value
    > End Sub
    > Public Property Name() As String
    > Get
    > Return _name
    > End Get
    > Set(ByVal Value As String)
    > _name = Name
    > End Set
    > End Property
    > Public Property Value() As String
    > Get
    > Return (_value)
    > End Get
    > Set(ByVal Value As String)
    > _value = Value
    > End Set
    > End Property
    > End Class
    >
    >
    > then using this function the name and ID is being inserted in the database
    >
    >
    >
    > Funtion LoopingControls(oControl As Control)
    > Dim frmCtrl As Control
    > oArrayList = New ArrayList
    > For each frmCtrl in oControl.Controls
    > If TypeOf frmCtrl Is TextArea Then
    >
    > Call FunctionDB(db)
    >
    > End If
    > If frmCtrl.HasControls Then
    > LoopingControls(frmCtrl)
    > End If
    > Next
    > End Function
    >
    >
    > This is the function which will do database connection.
    >
    > Function FunctionDB(db)
    >
    > Set db=Server.CreateObject ("ADODB.Connection")
    >
    > db.Open "DSN=surveydsn;User ID=dbo_sage;Password=sage;"
    >
    > SQL = "INSERT INTO survey ( i, res ) "
    > & " VALUES(" & i & ","
    > & "'" & Replace(res, "'", "''") & "')"
    > db.Execute SQL
    > db.Close
    > db.Execute(VarQuery)
    > End Function
    >
    >
    >
    > Now the thing is I am messed up with this, I have all the logic to
    > perform,but not being able to assemble it.
    >
    >
    > Lastly somebody told me to use Microsoft Visual studio 6, well i am not
    > being able to open interdev as I dont have FrontPage 98 server extension and
    > also Its not available on net free to download,
    >
    >
    > Hope you will again help me.
    >
    > Thank again for your support,it really helped me.
    >
    > "Old Pedant" wrote:
    >
    > > Using the FORM you actually show there, you would just alter my code to
    > >
    > > <%
    > > Set conn = Server.CreateObject("ADODB.Connection")
    > > conn.Open "...your connection string ..."
    > >
    > > For answerNumber = 1 TO 4
    > > answer = Request("Your Thoughts Q" & answerNumber)
    > > If answer <> "" Then
    > > SQL = "INSERT INTO answers( answerNumber, answer ) " _
    > > & " VALUES(" & answerNumber & "," _
    > > & "'" & Replace(answer, "'", "''") & "')"
    > > conn.Execute SQL
    > > End If
    > > Next
    > > conn.Close
    > > %>
    > >
    > > You didn't bother to show us the schema of your DB table, so I just assumed
    > > TABLE: answers
    > > answerNumber int
    > > answer text
    > >
    > >
    BLUESTAR, Jul 14, 2008
    #9
  10. BLUESTAR

    Old Pedant Guest

    RE: INSERT DATA FROM MULTIPLE TEXTAREAS IN THE FORM TO THE DATABAS

    "BLUESTAR" wrote:
    >
    > I forgot to say that,more questions will be added,but I dont know how
    > many,so i need to traverse the form in order to count # of textareas, so the
    > for loop from 1 to 4 will not work in that case


    So change the 4 to the actual number of questions.

    Or store the number of questions in another (hidden) form field.

    Or just change it to 99.

    The code in there will *ONLY* store answers that it finds to be non-blank.

    <%
    Set conn = Server.CreateObject("ADODB.Connection")
    conn.Open "...your connection string ..."

    For answerNumber = 1 TO 99
    answer = Trim( "" & Request("Your Thoughts Q" & answerNumber) )
    If answer <> "" Then ' *** SKIPS BLANK ANSWERS! ***
    SQL = "INSERT INTO answers( answerNumber, answer ) " _
    & " VALUES(" & answerNumber & "," _
    & "'" & Replace(answer, "'", "''") & "')"
    conn.Execute SQL
    End If
    Next
    conn.Close
    %>
    Old Pedant, Jul 18, 2008
    #10
  11. BLUESTAR

    Evertjan. Guest

    >>> RE: INSERT DATA FROM MULTIPLE ...



    =?Utf-8?B?T2xkIFBlZGFudA==?= wrote on 19 jul 2008 in
    microsoft.public.inetserver.asp.general:
    > <%
    > Set conn = Server.CreateObject("ADODB.Connection")
    > conn.Open "...your connection string ..."
    >
    > For answerNumber = 1 TO 99
    > answer = Trim( "" & Request("Your Thoughts Q" & answerNumber) )
    > If answer <> "" Then ' *** SKIPS BLANK ANSWERS! ***
    > SQL = "INSERT INTO answers( answerNumber, answer ) " _
    > & " VALUES(" & answerNumber & "," _
    > & "'" & Replace(answer, "'", "''") & "')"
    > conn.Execute SQL
    > End If
    > Next
    > conn.Close
    > %>


    Nice code, gives me 99 chances to do a SQL-injection i one go!

    And both in post and in querystring!


    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., Jul 19, 2008
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. MacEgan
    Replies:
    2
    Views:
    342
    Andrew Thompson
    Dec 17, 2005
  2. MacEgan
    Replies:
    1
    Views:
    290
    IchBin
    Dec 17, 2005
  3. Jeff Thies

    Page test: expando textareas

    Jeff Thies, Jul 2, 2004, in forum: HTML
    Replies:
    4
    Views:
    383
    Blinky the Shark
    Jul 2, 2004
  4. Replies:
    1
    Views:
    356
    Adrienne Boswell
    Mar 16, 2006
  5. Jeff

    Textareas in JSP

    Jeff, Feb 18, 2004, in forum: Javascript
    Replies:
    1
    Views:
    89
    kaeli
    Feb 18, 2004
Loading...

Share This Page