Introduction
These scripts will make variables live well beyond the page scope and make the debugging
process of passing variables between pages just a memory. All the scripts are very flexible
and work on form elements, query strings and cookies.
GetVariables
The first Function GetVariables(type1, type2)
can be used in place of the very common:-
var1=request.form("var1")
var2=request.form("var2")
var3=request.form("var3")
var4=request.form("var4")
var5=request.form("var5")
and likewise for reading in query strings and cookies
Taking forms as an example:
You simply now just call the function, and all vbscript values are set to match those
posted from a form. This sounds great, and it is, but the vbscript variables are named
identically to the form/querystring/cookie variable names (this is usually a good thing)
If for example a form contained a text box that is named 'Surname',
and after submitting the form, the following page called upon the GetVariables("form",0)
function, VBScript would now have access to a variable called Surname containing the value
of whatever the user entered into the textbox. It will loop through all form variables,
setting the VBScript equivalents.
SaveAsFormFields
The Second Function SaveAsFormFields(type1, type2)
is very useful for those occasions where
you need to read in all the form/querystring/cookie values, and include them within another form as
hidden fields. Reasons to this could include multiple page forms and also when a user
enters data incorrectly on a form, and you want to post the data back for editing, etc
SaveAsCookie
The Third Function SaveAsCookie(type1, type2)
will pass the variables stated by
type1 and type2
to a cookie on the clients computer. You can retrieve this cookie and read the variables
back in on another page using:
Call GetVariables("cookies",0)
Please email ASPwiz@hotmail.com with all feedback. (Including bugs if any)
I do not require a mention if you use this code (Unless the source is published), it
is supplied purely because
I know how much of a pain in the ass the long way round is and I feel sorry for those
who are none-the-wiser. Please feel free to distribute this as far and as wide as you like.
This message will self destruct, etc......
The Scripts
<%
function GetVariables(type1,type2)
if lcase(type1)="form" or lcase(type2)="form" or lcase(type1)="all" then
For Each Field In Request.Form
TheString = Field & "=Request.Form(""" & Field & """)"
Execute(TheString)
Next
end if
if lcase(type1)="cookies" or lcase(type2)="cookies" or lcase(type1)="all" then
For Each Field In Request.cookies
TheString = Field & "=Request.cookies(""" & Field & """)"
Execute(TheString)
Next
end if
if lcase(type1)="querystring" or lcase(type2)="querystring" or lcase(type1)="all" then
For Each Field In Request.querystring
TheString = Field & "=Request.querystring(""" & Field & """)"
Execute(TheString)
Next
end if
END function
function SaveAsFormFields (type1, type2)
if lcase(type1)="form" or lcase(type2)="form" or lcase(type1)="all" then
For Each Field In Request.Form
TheString="<input type=""hidden"" name=""" & Field & """ value="""
Value=Request.Form(Field)
Thestring=TheString + cstr(Value) & """>" & vbcrlf
Response.Write TheString
Next
end if
if lcase(type1)="cookies" or lcase(type2)="cookies" or lcase(type1)="all" then
For Each Field In Request.cookies
TheString="<input type=""hidden"" name=""" & Field & """ value="""
Value=Request.cookies(Field)
Thestring=TheString + cstr(Value) & """>" & vbcrlf
Response.Write TheString
Next
end if
if lcase(type1)="querystring" or lcase(type2)="querystring" or lcase(type1)="all" then
For Each Field In Request.Querystring
TheString="<input type=""hidden"" name=""" & Field & """ value="""
Value=Request.querystring(Field)
Thestring=TheString + cstr(Value) & """>" & vbcrlf
Response.Write TheString
Next
end if
END function
function SaveAsCookie (type1, type2)
if lcase(type1)="form" or lcase(type2)="form" or lcase(type1)="all" then
For Each Field In Request.Form
Response.cookies(field)= Request.Form(Field)
Next
end if
if lcase(type1)="cookies" or lcase(type2)="cookies" or lcase(type1)="all" then
For Each Field In Request.cookies
Response.cookies(field)= Request.cookies(Field)
Next
end if
if lcase(type1)="querystring" or lcase(type2)="querystring" or lcase(type1)="all" then
For Each Field In Request.Querystring
Response.cookies(field)= Request.querystring(Field)
Next
end if
END function
%>
Testing the Code
Set up these scripts in a page (or use the supplied downloadable page), plus ensure that you
include some test code to call the functions. For example:
call getvariables("all",0)
call SaveAsFormFields("all",0)
call SaveAsCookie("all",0)
response.write vbcrlf & test1 & " " & test2 & " " & test3 & vbcrlf
response.write "Cookies" & vbcrlf
for each item in request.cookies
response.write(Request.cookies(item))& " " & vbcrlf
next
response.write "<B>Click View...source to see where hidden form elements have been set.</b>"
Also create a form or something
to call this page, passing variables using which ever method you choose.
Once the script is run, checking the resulting HTML source
will reveal the result of the saveasformfields
function.
Try typing:
formfunctions.asp?test1=testing&test2=numbers12345&test3=final-test
Into the browser directly (where formfunctions.asp is the name of the page with these scripts).
Updates
11 June 2001 - The functions have been overhauled completely and are now more useful than
ever. The complete scripts are as follows:
<%
Function SetVars(StrType)
If lcase(StrType) = "form" or lcase(strType) = "all" then
For Each Field in Request.Form
TheString = Field & "=Request.Form(""" _
& Field & """)"
EXECUTE(TheString)
Next
End If
If lcase(StrType) = "querystring" or lcase(strType) = "all" then
For Each Field in Request.Querystring
TheString= Field & "Request.Querystring(""" _
& Field & """)"
EXECUTE(TheString)
Next
End If
If lcase(StrType) = "cookies" or lcase(strType) = "all" then
For Each Field in Request.Cookies
TheString= Field & "Request.Cookies(""" _
& Field & """)"
EXECUTE(TheString)
Next
End If
END Function
Function IncludeHidden(StrType, IGNORELIST)
If lcase(StrType) = "form" or lcase(StrType) = "all" then
For each Field in Request.Form
If NOT Onlist(Field, IGNORELIST) Then
TheString="<Input Type=""HIDDEN"" Name=""" _
& Field & """ Value="""
StrValue=Request.Form(Field)
TheString=TheString + cstr(StrValue) & """>" & VbCrLf
Response.Write TheString
End If
Next
End If
If lcase(StrType) = "querystring" or lcase(StrType) = "all" then
For each Field in Request.Querystring
If NOT Onlist(Field, IGNORELIST) Then
TheString="<Input Type=""HIDDEN"" Name=""" _
& Field & """ Value="""
StrValue=Request.Querystring(Field)
TheString=TheString + cstr(StrValue) & """>" & VbCrLf
Response.Write TheString
End If
Next
End If
If lcase(StrType) = "cookies" or lcase(StrType) = "all" then
For each Field in Request.Cookies
If NOT Onlist(Field, IGNORELIST) Then
TheString="<Input Type=""HIDDEN"" Name=""" _
& Field & """ Value="""
StrValue=Request.Cookies(Field)
TheString=TheString + cstr(StrValue) & """>" & VbCrLf
Response.Write TheString
End If
Next
End If
END Function
Function WriteQueryString(StrType, IGNORELIST)
If lcase(StrType) = "form" or lcase(StrType) = "all" then
For each Field in Request.Form
If NOT Onlist(Field, IGNORELIST) Then
TheString=TheString+Field&"="& Request.Form(Field) & "&"
End If
Next
End if
If lcase(StrType) = "querystring" or lcase(StrType) = "all" then
For each Field in Request.Querystring
If NOT Onlist(Field, IGNORELIST) Then
TheString=TheString+Field &"=" & Request.Querystring(Field) & "&"
End If
Next
End if
If lcase(StrType) = "cookies" or lcase(StrType) = "all" then
For each Field in Request.Cookies
If NOT Onlist(Field, IGNORELIST) Then
TheString=TheString+Field&"="& Request.Cookies(Field) & "&"
End If
Next
End if
If right(TheString,1)="&" Then TheString=Left(TheString,Len(TheString)-1)
Response.Write TheString
END Function
Function WriteCookies(StrType, IGNORELIST)
If lcase(StrType) = "form" or lcase(StrType) = "all" then
For each Field in Request.Form
If NOT Onlist(Field, IGNORELIST) Then
Response.Cookies(Field)=Request.Form(Field)
Response.Cookies(Field).Expires = now() + 60
End If
Next
End if
If lcase(StrType) = "querystring" or lcase(StrType) = "all" then
For each Field in Request.QueryString
If NOT Onlist(Field, IGNORELIST) Then
Response.Cookies(Field)=Request.QueryString(Field)
Response.Cookies(Field).Expires = now() + 60
End If
Next
End if
END Function
Function Onlist(StrField,StrIgnoreList)
TheArray=Split(StrIgnorelist,",")
If isarray(TheArray) Then
For count=LBound(TheArray) to UBound(TheArray)
If lcase(StrField) = lcase(TheArray(Count)) Then
Onlist=True
End If
Next
End If
End Function
%>