Two Functions: ReadFormVariables and SendHiddenFormFields

Contributed by
Rating: 5 stars5 stars5 stars5 stars5 stars / 2
March 16, 2001
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

Two Functions:ReadFormVariables and SendHiddenFormFields

<%
'===================================================
'The Functions below were developed by Robert Collyer:- ASPwiz@hotmail.com
'
'These scripts will now make variables live well beyond the page scope and make the debugging
'process of passing variables between pages just a memory.
'All scripts are really flexible and work on form elements, querystrings and cookies.
'
'====================================================
'The first Funtion-'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 querystrings 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 equivelents.
'
'
'===================================================
'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
'
'===================================================
'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 arse the long way round is and I feel sorry for those
'who are non-the-wiser. Please feel free to distribute this as far and as wide as you like.
'This message will self destruct, etc......
'==================================================

'*//**//**//**//**//**//**//**//**//**//**//**//**//**//**//**//**//**
   
'==================================================    
' 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
    'uncomment the code below to test the functions.
    'Set up a form or something to call this page passing variables
    'or manually pass querystring info when calling this page
    '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.
    'Uncomment below here
    '
    'call getvariables("all",0)
    'call SaveAsFormFields("all",0)
    'call SaveAsCookie("all",0)
    'response.write vbcrlf & test1 & "<BR>" & test2 & "<BR>" & test3 & vbcrlf
    'response.write "<BR><BR>Cookies<BR>-------<BR>" & vbcrlf
    'for each item in request.cookies
    '     response.write(Request.cookies(item))&"<BR>" & vbcrlf
    'next
    'response.write "<B><BR>Click View...source to see where hidden form elements have been set.</b>"
%>

blog comments powered by Disqus
ASP CODE ARTICLES

- ASP Forms
- ASP: The Beginning
- Getting Remote Files With ASP Continued
- Inbox and Outbox Manipulation in ASP
- Relational DropDownList Using VB.NET
- Ad Tracking URL Hits
- Use ViewState to display one record per page...
- Send Email using ASP.NET formatted in HTML
- ASP File Explorer
- ASP/XML Interview questions by Srivatsan Sri...
- Pressing RETURN won't submit the form
- This shows how you get the TEXT of a combo r...
- Group Data by Adrian Forbes
- Multiple checkbox select sample
- Multiple checkbox select with all values sam...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 8 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials