便捷的表单函数






2.40/5 (4投票s)
2001年4月3日
2分钟阅读

92250

1034
这些脚本将使变量的生命周期远远超出页面范围,并将页面间传递变量的调试过程变得易如反掌。
引言
这些脚本将使变量的生命周期远远超出页面范围,并将页面间传递变量的调试过程变得易如反掌。所有脚本都非常灵活,适用于表单元素、查询字符串和 Cookie。GetVariables
第一个函数 GetVariables(type1, type2)
可以代替非常常见的:
var1=request.form("var1")
var2=request.form("var2")
var3=request.form("var3")
var4=request.form("var4")
var5=request.form("var5")
以及读取查询字符串和 Cookie 的方式
以表单为例
你现在只需调用该函数,所有 VBScript 值都将设置为与从表单提交的值相匹配。听起来很棒,而且确实如此,但 VBScript 变量的名称与表单/查询字符串/Cookie 变量名称相同(通常这是好事)。例如,如果表单包含一个名为“Surname”的文本框,并在提交表单后,下一页调用了 GetVariables("form",0)
函数,VBScript 现在将可以访问一个名为 Surname 的变量,其中包含用户在文本框中输入的值。它将循环遍历所有表单变量,设置 VBScript 等效项。
SaveAsFormFields
第二个函数 SaveAsFormFields(type1, type2)
对于需要读取所有表单/查询字符串/Cookie 值,并将它们包含在另一个表单中作为隐藏字段的场合非常有用。这样做原因包括多页表单,以及当用户在表单中错误地输入数据,并且你希望将数据发布回来进行编辑等。
SaveAsCookie
第三个函数 SaveAsCookie(type1, type2)
将由 type1 和 type2 指定的变量传递到客户端计算机上的 Cookie。你可以检索此 Cookie,并在另一页中使用以下方式读取变量:
Call GetVariables("cookies",0)
请将所有反馈(包括任何错误)发送至 ASPwiz@hotmail.com。如果您使用此代码,我不需要提及(除非发布了源代码),它纯粹是因为我知道绕远路有多么痛苦,并且我为那些对此一无所知的人感到遗憾。请随意尽可能广泛地分发此代码。
此消息将自我销毁,等等......
脚本
<% '*//**//**//**//**//**//**//**//**//**//**//**//**//**//**//**//**//** '======================================================================================================= ' 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 '*//**//**//**//**//**//**//**//**//**//**//**//**//**//**//**//**//** %>
测试代码
将这些脚本设置在一个页面中(或使用提供的可下载页面),并确保包含一些测试代码来调用这些函数。例如
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>"
另外创建一个表单或类似的东西来调用此页面,使用你选择的任何方法传递变量。脚本运行后,检查生成的 HTML 源代码将显示 saveasformfields
函数的结果。
尝试输入
formfunctions.asp?test1=testing&test2=numbers12345&test3=final-test
直接在浏览器中(其中 formfunctions.asp 是包含这些脚本的页面的名称)。
更新
2001 年 6 月 11 日 - 这些函数已经完全进行了修改,现在比以往任何时候都更有用。完整的脚本如下
<% '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 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) 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 %>