ASP 101 - Active Server Pages 101 - Web01
The Place ASP Developers Go!



Windows Technology Windows Technology
15 Seconds
4GuysFromRolla.com
ASP 101
ASP Wire
VB Forums
VB Wire
WinDrivers.com
internet.commerce internet.commerce
Partners & Affiliates
ASP 101 is an
internet.com site
ASP 101 is an internet.com site
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

ASP 101 News Flash ASP 101 News Flash



 Top ASP 101 Stories Top ASP 101 Stories
What is Adovbs.inc and Why Do I Need It?
An Overview of ASP.NET
Connections, Commands, And Procedures

QUICK TIP:
Use design-time controls
Show All Tips >>
ASP 101 RSS Feed ASP 101 Updates


Some Nifty Functions for Passing Variables Around

Some Nifty Functions for Passing Variables Around


Introduction

If you're coming here directly and haven't read A Pair of Functions for Multi-Page Forms, you should do so now so you know what you're getting into. What follows is a set of extremely helpful code that should make your life a lot easier.

Since I'm always accused of not getting to the point quickly enough... that's all I'm saying. I'll let the code can speak for itself.

The Code

<%
'//////    These Functions were written by
'//////    Robert Collyer:-  ASPwiz@hotmail.com
'
'//////    Please report any problems found to me ASAP, so I may fix
'//////    them and build another release.  I get literally hundreds
'//////    of emails asking how to use these functions, and questions
'//////    relating to them, etc.  If you have any enquiries regarding
'//////    these scripts, email me, but please allow up to 36 Hours
'//////    for a response.
'
'//////    If you are interested in functions similar to the above but
'//////    allow interaction with databases, then these will be
'//////    available soon.
'
'//////    I hope you find these functions to be as useful and
'//////    time-saving as everyone else, and use them to their fullest
'//////    potential.  I TRULY APPRECIATE ANY FEEDBACK.
'//////
'//////
'//////    The GetVars function, will ONLY work with the VBScript
'//////    Engine ver 5.x.  Updates to the scripting engine for win
'//////    9x, and NT 4.0 are available from the microsoft site:
'//////    http://www.microsoft.com/scripting
'//////
'//////    Bear in mind, I always find it nice to get a mention where
'//////    my functions are used but I do not require it.  I am more
'//////    willing to help those and supply new updated functions to
'//////    those who acknowledge me in either content, or comments
'//////    with the page source.
'//////
'//////    Good Luck, and Happy Coding!!!
'//////
'//////    Rob Collyer    (aka ASPwiz)
'//////    ASPwiz@hotmail.com
'
'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")
'
'Usage:-  Call SetVars(StrType) 
'   Where 'StrType' is a string value of either, "form",
'   "querystring", "cookies", or "all"
'---------------------------------------------------------------------
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
'---------------------------------------------------------------------
'---------------------------------------------------------------------
'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 </form> 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.
'
'Usage:-  Call IncludeHidden(StrType, IGNORELIST)
'   Where 'StrType' is a string value of either, "form",
'   "querystring", "cookies", or "all"
'   and 'IGNORELIST' is a comma seperated string of field names to
'   ignore. (Case INsensitive)
'
'EXAMPLE:-  To include all values submitted to the page via 
'a form, within the second form as form fields...
'
'	<Form Action="Form3.asp" Method="post">
'		<Input type="text" name="whatever">
'		<!-- More fields as appropriate-->
'		<!-- ............ -->
'		
'		<$ Call IncludeHidden("Form", "") $>
'		<Input type="submit">
'	</Form>
'
'  If you wanted to exclude a field called 'whatever'
'       (Perhaps the page receives its own data back at some stage)
'  Then you could use:-  <$ Call IncludeHidden("Form", "whatever") $>
'     and the hidden form field named 'whatever' WONT be included
'---------------------------------------------------------------------
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
'---------------------------------------------------------------------
'---------------------------------------------------------------------
'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.
'
'Usage:-  Call WriteQueryString(StrType, IGNORELIST) 
'	Where 'StrType' is a string value of either, "form",
'   "querystring", "cookies", or "all"
'   and 'IGNORELIST' is a comma seperated string of field names to
'   ignore. (Case INsensitive)
'
'EXAMPLE:- To write all values submitted to the page via a
'          querystring...
'
'  <A href="APage.asp?<$Call WriteQueryString("querystring","")$>">
'   Click Here</a>
'
'  If you wanted to exclude a field for whatever reason, then follow
'  the example as for the 'IncludeHidden' 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)
	End If
    Response.Write TheString
END Function
'---------------------------------------------------------------------
'---------------------------------------------------------------------
'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.  
'    
'Usage:-  Call Writecookies(StrType, IGNORELIST) 
'   Where 'StrType' is a string value of either, "form",
'   "querystring", or "all" and 'IGNORELIST' is a comma seperated
'   string of field names to ignore. (Case INsensitive)
'
'EXAMPLE:-  To write a cookie of all values submitted to the 
'           page via a form...
'
'    <$ Call WriteCookies("Form","") $>
'
'   ALL COOKIES WILL EXPIRE AFTER 60 DAYS
'    You may change this setting below. 
'
'  If you wanted to exclude a field for whatever reason, then follow
'  the example as for the 'IncludeHidden' 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)
                'Set the cookie to expire 60 days from now
                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)
                'Set the cookie to expire 60 days from now
                Response.Cookies(Field).Expires = now() + 60
            End If
        Next
    End if
END Function
'---------------------------------------------------------------------
' This function is needed by the other functions, and must also be
' included in any files which use the IncludeHidden, WriteQuerystring
' and WriteCookies Functions.
'---------------------------------------------------------------------
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
%>

Getting The Code

As always you can simply cut and paste the code from above, but in case that's giving you trouble, I've zipped up the file so you can download it directly.


Home |  News |  Samples |  Articles |  Lessons |  Resources |  Forum |  Links |  Search |  Feedback

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers