The Rambling, Long-Winded Introduction by the ASP 101 Webmaster
Being the webmaster of ASP 101 sort of opens me
up to getting a lot of email. While the
endless questions do get tiresome at times, the position also gives me the
opportunity to see a lot of really cool code. Some of this code comes in the form of
a question with a piece of broken code attached, while other pieces of
code are fully functional scripts that a visitor simply wanted to share.
I quickly discovered that I needed to do something with all this code
I was being sent. It seemed like a real waste to have it sit in my email
client, but the site didn't really have a place for it.
So... we created the Visitor Contributions (VC) Page on which I've been publishing
some of the better written and more interesting scripts that come across my
(virtual) desk.
The piece of code in question came to me like all the others...
attatched to an email. It seemed relatively simple but it was well written
and I almost just posted it to the previously mentioned VC Page. The email
that accompanied the code didn't give much of a description so
I took a quick look at the script in order to write up a breif
description. That's when it dawned upon
me how useful this little code snippet actually was... which brings us
up to date and explains why this code snippet has gotten it's own page
on our site. I hate to do this since it hardly seems fair to
the so many other great pieces of code still buried
(hopefully not too deeply!) on the VC Page, but it felt like the right
thing to do.
Sometimes the best things in life really are the simple ones.
Ohh... and the free ones too! ;)
Why You Need This Code
Now that I've rambled on through the introduction, I'm going to
not go into any more detail about anything! So without explaining
why, most people quickly rule out cookies and sessions when they
need to design multi-page forms. This leaves hidden form fields and
a DB as the two remaining mainstream options. I also won't debate the
merits of each approach, but let's say for argument's sake that you
choose hidden form fields. That leaves you with a lot of work to do.
On each page of the form you need to read in all the fields and values
from the previous page and place them on the new page.
Well... not any more.
The Email
Hi I apologise for being too hectically busy to actually write an article for your site, but I have attached two functions I know everyone will think are a true Godsend.
They will save tons and tons of developers time and make use of new features in VBScript version 5.
I have commented the code and I'm sure all will be self explanatory.
I only ask that if this code is published on your site, I get mentioned along with my email address. I DO NOT however require links, or a mention by those who use the code in their own pages.
Hope this proves as useful to your visitors as it has to me in the last week since I wrote it.
Besides some formatting, I've left the code pretty much alone...
<%
'The Two functions below were developed by
'Robert Collyer:- ASPwiz@hotmail.com
'
'The First Function-'ReadFormVariables' 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")
'....etc
'
'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 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 'ReadFormVariables'
'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-'SendHiddenFormFields' is very useful for those
'occasions where you need to read in all the form 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
'
'Please email ASPwiz@hotmail.com with all feedback.
'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......
Sub SendHiddenFormFields ()
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
NextEnd SubSub ReadFormVariables()
For Each Field In Request.Form
TheString = Field & " = Request.Form(""" & Field & """)"
Execute(TheString)
NextEnd Sub
%>
Getting The Code
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.