问题
EDIT: I now realise after the help from those who replied that my question was about whether Request.Form Data is a string in the same way that a$="FooBar" is a string and the Array command in Classic ASP.
I'm trying to make an Array from data submitted in a Form.
The form fields are dynamically created and have the same name "subj".
The Response.Write(Request.Form("subj")) produces:
"Welcome and Introduction, Talk1, Talk2 ,Interactive review of the conference"
When I check the TypeName or VarType Request.Form("subj") is a string. Then I code:
subjs = """" & Replace(Request.Form("subj"), ", ", """,""") & """"
subjects = Array(subjs)
With the intention to give:
subjs = "Welcome and Introduction","Talk1","Talk2","Interactive review of the conference"
subjects(0) = Welcome and Introduction
subject(1) = Talk1
subject(2) = Talk2
subject(3) = Interactive review of the conference
The problem is that what I actually get is:
subjs = "Welcome and Introduction","Talk1","Talk2","Interactive review of the conference"
subject(0) = "Welcome and Introduction","Talk1","Talk2","Interactive review of the conference"
For some reason the Array isn't correctly formed as there is no subject(1) subject(2) or subject(3).
If I physically copy and paste the output of subjs into my code, then Array works fine but I can't get the Array to work on Form Data.
I've tried using CStr and checked all of the quotation marks.
Why doesn't it work?
Thank you to those who took the trouble to reply. Whilst Split does work in fields without commas, SET var = Request.Form("subj") as per @Kul-Tigin, I think is the key but would be keen to hear other thoughts
回答1:
Since the request collection values may contain commas, using split can cause unespected results.
Creating an array through the collection is more reliable.
Set subject = Request.Form("subj")
ReDim subjects(subject.Count - 1)
For i = 1 To subject.Count
subjects(i - 1) = subject(i)
Next
回答2:
The Array function expects a comma-separated list: "words","stuff","foo", but what you get from a Request.Form is more like "words,stuff,foo".
Ultimately, though, it doesn't matter, because as you've noted in your comment, the appropriate function to use is Split.
来源:https://stackoverflow.com/questions/40674060/array-from-request-form-in-classic-asp