Click here to Skip to main content
12,452,721 members (54,389 online)
Click here to Skip to main content
Add your own
alternative version

Stats

83.5K views
994 downloads
42 bookmarked
Posted

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>

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

You may also be interested in...

Pro
Pro

Comments and Discussions

 
Rant[My vote of 1] Dangerous Injection Vulnerability Pin
greg_bradley20-May-09 20:17
membergreg_bradley20-May-09 20:17 
QuestionWould you add support for form arrays? Pin
jasonpc3po6-Oct-07 19:57
memberjasonpc3po6-Oct-07 19:57 
GeneralThank You &amp; A Question Pin
bphoward116-Oct-04 12:22
memberbphoward116-Oct-04 12:22 
Generalthanks Robbie Pin
brahman17-Apr-03 23:38
memberbrahman17-Apr-03 23:38 
GeneralThis is great! The improved scripts are a real time saver! Pin
Mike Whitenton2-Jul-01 15:13
memberMike Whitenton2-Jul-01 15:13 
QuestionWhat? Pin
sjoe903-Apr-01 0:17
membersjoe903-Apr-01 0:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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 | Terms of Use | Mobile
Web02 | 2.8.160826.1 | Last Updated 11 Jun 2001
Article Copyright 2001 by Robert Collyer
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid