Reading Form values into a Scripting.Dictionary

T

Tim Chmielewski

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.
 
S

Scott McNair

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.
 
T

Tim Chmielewski

"=?Utf-8?B?U3RlcGhlbk1jQw==?=" <[email protected]>
wrote in
- 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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top