Reading Form values into a Scripting.Dictionary

Discussion in 'ASP General' started by Tim Chmielewski, Jun 21, 2004.

  1. I have been trying to read values from a form into an array on a page
    without much success and was told to use a Scripting.Dictionary instead.

    There are a variable number of fields on the form as it is different
    according to which product is ordered (as are the fields that are used to
    create the record.)

    This is my first attempt:
    set OrderInfo = Server.CreateObject("scripting.dictionary")
    set OrderInfo("prodid") = Server.CreateObject("scripting.dictionary")
    set OrderInfo("userid") = Server.CreateObject("scripting.dictionary")
    OrderInfo("prodid") = lngProductID
    OrderInfo("userid") = lngUID

    For each x in Request.Form
    set OrderInfo("prodid")("userid")(x) = Server.CreateObject
    ("scripting.dictionary")
    OrderInfo("prodid")("userid")(x) = Request.Form(x)
    Next

    For each x in OrderInfo("prodid")("userid")
    Response.Write x & " = " & OrderInfo("prodid")("userid")(x) & "<BR>" &
    vbcrlf
    Next


    Thanks.
    --
    GIT Groupie : http://gitgroupie.timchuma.com
    The Twits Give Me the Shits : http://twitsgivemetheshits.timchuma.com
    My Photos : http://photos.timchuma.com
    Hong Kong Movie Reviews: http://hkmovies.timchuma.com
     
    Tim Chmielewski, Jun 21, 2004
    #1
    1. Advertising

  2. Tim Chmielewski

    Scott McNair Guest

    Tim Chmielewski <> wrote in
    news:Xns950F6DB987B6Fchumadcsinetau@130.133.1.4:

    > I have been trying to read values from a form into an array on a page
    > without much success and was told to use a Scripting.Dictionary
    > instead.
    >
    > There are a variable number of fields on the form as it is different
    > according to which product is ordered (as are the fields that are used
    > to create the record.)


    Wow, that's a lot of scripting.dictionary calls.

    Without being totally sure what you're doing, I think you could probably
    simplify it down to something like this:

    Set OrderInfo = Server.createObject("scripting.dictionary")
    For Each Item in Request.Form
    OrderInfo(Item) = Request.Form(Item)
    Next

    That way, if for example you had form items ProductID, Color, and Size,
    you would reference them via

    Response.Write OrderInfo("ProductID")
    Response.Write OrderInfo("Color")
    Response.Write OrderInfo("Size")

    Hope that helps.
     
    Scott McNair, Jun 22, 2004
    #2
    1. Advertising

  3. "=?Utf-8?B?U3RlcGhlbk1jQw==?=" <>
    wrote in news::


    > - array example:
    > ....Read into dict:
    > Dim oArray()
    > ReDim oArray(Request.Form.Count)
    > For i = 0 To Request.Form.Count
    > oArray(i) = Request.Form(i)
    > Next
    >

    I ended up using your array example:
    ' Store Extra Product Fields
    Dim ProductFieldsArray()
    ReDim ProductFieldsArray(intNumFields)
    strSQL = "SELECT FieldName FROM tblProductExtraFields WHERE ProductCode
    Like '" & strPC & "'"
    Set rstProduct = Server.CreateObject("ADODB.Recordset")
    rstProduct.Open strSQL, conConnection
    i = 0
    Do While Not rstProduct.EOF
    ProductFieldsArray(i) = rstProduct("FieldName")
    rstProduct.MoveNext
    i = i + 1
    Loop
    rstProduct.Close

    ' Store form values
    Dim oArray()
    ReDim oArray(Request.Form.Count)
    For i = 0 To Request.Form.Count
    oArray(i) = Request.Form(i)
    Next

    intStopVal = Ubound(oArray)
    intStartVal = 4
    intSet = Ubound(ProductFieldsArray) - 1
    intSetStop = intStartVal + intSet

    strSQLStart = "INSERT INTO tblCartExtraInfo(lngUserID, lngProductID"

    For n = 0 to Ubound(ProductFieldsArray) - 1
    strSQLStart = strSQLStart & ", " & ProductFieldsArray(n)
    Next
    strSQLStart = strSQLStart & ") VALUES ('" & lngUID & "','" & lngProductID
    & "',"

    ' Checking input values & insert
    For i = 0 to 4
    strSQLValues = ""
    intHasVals = 0
    For j = intStartVal to intsetStop
    strSQLValues = strSQLValues & "'" & oArray(j) & "',"
    if len(oArray(j)) > 0 then
    intHasVals = intHasVals + 1
    end if
    Next
    strSQLValues = Mid(strSQLValues,1,len(strSQLValues) - 1) & ")"
    strSQLFull = strSQLStart & strSQLValues
    intStartVal = intSetStop + 1
    intSetStop = intStartVal + intSet
    if intHasVals = intSet + 1 then
    conConnection.Execute(strSQLFull)
    end if
    Next


    Thanks.
    --
    GIT Groupie : http://gitgroupie.timchuma.com
    The Twits Give Me the Shits : http://twitsgivemetheshits.timchuma.com
    My Photos : http://photos.timchuma.com
    Hong Kong Movie Reviews: http://hkmovies.timchuma.com
     
    Tim Chmielewski, Jun 23, 2004
    #3
    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. =?Utf-8?B?QWRuYW4=?=

    Scripting.Dictionary alternative

    =?Utf-8?B?QWRuYW4=?=, Aug 4, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    4,701
    Michael O'Donovan [MSFT]
    Aug 4, 2004
  2. Ron Stephens
    Replies:
    23
    Views:
    2,981
    Ron Stephens
    Apr 12, 2004
  3. DaveInSidney
    Replies:
    0
    Views:
    446
    DaveInSidney
    May 9, 2005
  4. Robin

    scripting.Dictionary Objects

    Robin, Apr 22, 2004, in forum: ASP General
    Replies:
    3
    Views:
    523
    Ray at
    Apr 23, 2004
  5. Rodd Snook
    Replies:
    8
    Views:
    355
    Rodd Snook
    May 28, 2004
Loading...

Share This Page