问题
Hi I have a form and the action is submitting into a script named autoform.asp. In this script I usually request the fields values like this: txtFirstName = Request.Form("txtFirstname")
Now I want to make this to work dynamically without hard coding the requests in the script where the form is posting.
I managed to do this code to get the the form fields dynamically but they are not being loaded on the fly.
For Each Item In Request.Form
fieldName = Item
fieldValue = Request.Form(Item)
Response.Write(""& fieldName &" = Request.Form("""& fieldName &""")")
Next
The Response.Write is not good because it is stopping my script to continue. That said the values showing are correct. Can someone help me please? Maybe I need an array? and If yes how I can continue this process? THANKS
回答1:
EDIT as per comment below
You can use the Execute statement to mimic import_request_variables from php
For Each Item In Request.Form
ItemValue = Request.Form(Item)
Execute Item & " = """ & ItemValue & """"
Next
If you are intending to output
Firstname = Request.Form("Firstname")
then use
<%
For Each Item In Request.Form
fieldName = Item
fieldValue = Request.Form(Item)
Response.Write(fieldName & " = Request.Form(""" & fieldName & """)")
Next
%>
If you are intending to output
Firstname = "John"
then use
<%
For Each Item In Request.Form
fieldName = Item
fieldValue = Request.Form(Item)
Response.Write(fieldName & " = """ & fieldValue & """")
Next
%>
回答2:
I think you might be able to work something out using Execute(), eg
<%@ language = "VBScript" CodePage = 65001 %>
<%
option explicit
dim q
for each q in Request.QueryString
dim s
s = "dim " & q & vbCrLf & _
q & " = """ & Request.QueryString(q) & """" & vbCrLf & _
"Response.Write ""Value of " & q & " is "" & " & q & vbCrLf
%>
<pre><%= s %></pre>
<hr/>
<%
Execute s
%><hr/><%
next
%>
(I've used the querystring collection so it's easier to test). So input like
http://.../test-asp.asp?p=1&c=328904
will generate something like
dim p
p = "1"
Response.Write "Value of p is " & p
Value of p is 1
dim c
c = "328904"
Response.Write "Value of c is " & c
Value of c is 328904
it depends on what you actually need to do.
来源:https://stackoverflow.com/questions/9093918/requesting-field-names-and-values-from-form-and-add-them-on-the-fly-in-a-script