Professional Documents
Culture Documents
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")
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
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.
The Scripts
<%
'*//**//**//**//**//**//**//**//**//**//**//**//**//**//**//**//**//**
'===============================================================================
========================
' Call the following function with:
'
' Call GetVariables("TYPE1", "TYPE2") replacing TYPE1 and TYPE2 with: form,
cookies or querystring.
'
' Alternatively the function may be called as follows:
' Call GetVariables("ALL", 0) calling the function in this way performs all
three types.
'===============================================================================
========================
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)'Executes the command contained in the string(This
will set all VBScript variables)
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)'Executes the command contained in the string(This
will set all VBScript variables)
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)'Executes the command contained in the string(This
will set all VBScript variables)
Next
end if
END function
'*//**//**//**//**//**//**//**//**//**//**//**//**//**//**//**//**//**
'=============================================================================
' Call the following function with:
' Call SaveAsFormFields("TYPE1", "TYPE2") replacing TYPE1 and TYPE2 with: form,
cookies or querystring.
'
'Lastly the function may be also be called as follows:
'Call SaveAsFormFields("ALL", 0) calling the function in this way performs all
three types.
'=============================================================================
'*//**//**//**//**//**//**//**//**//**//**//**//**//**//**//**//**//**
'=============================================================================
' Call the following function with:
' Call SaveAsCookie("TYPE1", "TYPE2") replacing TYPE1 and TYPE2 with: form,
cookies or querystring.
'
'Lastly the function may be also be called as follows:
'Call SaveAsCookie("ALL", 0) calling the function in this way performs all three
types.
'=============================================================================
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:
<%
'All of these functions will come into their own when dealing with multiple
page
'forms.
'
'In the Examples given, remember to replace the <$ and $> tags
'with the proper open/close ASP tags.
'-------------------------------------------------------------------------------
--------------
'-------------------------------------------------------------------------------
--------------
'This first function will get variables from forms and/or querystrings
and/or cookies.
'The function will set VBScript variables with identical names, and equal
values.
'This saves the tedious task of request.form("this")
request.Querystring("that"),
'and request.cookies("the_other")
'This second function will get variables from forms and/or querystrings
and/or cookies,
'and write them in a form as hidden fields... This is very useful when
dealing with
'multiple page forms.
'The function must be called prior to the
tag, so it can include the hidden fields
'This saves the tedious task of: <Input type="hidden" name="field1"
value="value1">, etc
'This saves a lot of work when passing many, many variables.
'This third function will get variables from forms and/or querystrings
and/or cookies,
'and write them in a link as a querystring... This is very useful when
dealing with
'online applications that pass lots of variables this way.
'The function must be called in a specific way, example given below.
'This fourth function will get variables from forms and/or querystrings,
'and write them to the client as a cookie... This is often a method
'used for storing variables (Not my preference)
'Online applications that store lots of cookies will benefit from this
function.
'////// If you are interested in functions similar to the above but allow
interaction
'////// with databases, then these will be available soon.
Robert Collyer