Click here to Skip to main content
Click here to Skip to main content

Handy Form Functions

, 10 Jun 2001
Rate this:
Please Sign up or sign in to vote.
These scripts will make variables live well beyond the page scope and make the debugging process of passing variables between pages just a memory.
<!-- Download Links --> <!-- Main HTML starts here -->

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

<%
'*//**//**//**//**//**//**//**//**//**//**//**//**//**//**//**//**//**
    
'=======================================================================================================    
' 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.
'=============================================================================

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

'*//**//**//**//**//**//**//**//**//**//**//**//**//**//**//**//**//**

'=============================================================================
' 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")

    '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="/KB/asp/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)
    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)
                Response.Cookies(Field).Expires = now() + 60 'Set the cookie to expire 60 days from now
            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 'Set the cookie to expire 60 days from now
            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 WiteCookies 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
        

'//////    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 VBScripting 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
%>

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Robert Collyer

United States United States
No Biography provided

Comments and Discussions

 
QuestionWhat? Pinmembersjoe903-Apr-01 0:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.141022.2 | Last Updated 11 Jun 2001
Article Copyright 2001 by Robert Collyer
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid