HTML Forms
 A basic introduction to using forms. Breakdown of a forms anatomy, how they are submitted and processing the forms information.

Website: About Us ◄    ► Portfolio ◄    ► Contact Us

 







Website Hosting
Website Design
CDROM Productions
SEO
Database Design
Business ISP Services
Residential ISP Services

 

 





SEO Tips
Search Engines
Forms
Frames
ASP
 




About Us
Portfolio
Contact Us
Articles


Home

 
 

Included in these pages are small but functional code snippets in ASP. PLEASE be aware that I am not an IT Coding GURU rather I am self taught. Most of this code will not be exactly as an actual IT Guru would possibly have used, but it is functional, and hopefully written in such a way as to be used by you as a learning tool. These pages are how I would have liked to have code explained to me when I was learning!

Please Note: The following code snippets are Copy written to Vortex Media. You can use this code free of charge as long as the commented sections containing the copyright information (if used) remains intact. Please include a link back to www.vortexmedia.com.au if you use this code. By using the code below you agree that Vortex Media are not liable to you for any loss or damage ---- you know the drill, if you don't then read our disclaimer.

 
 

Let me know if this code was at all helpful!

Introduction To ASP (Active Server Pages)


ASP is a scripting language created by Microsoft. ASP enables you to combine HTML with powerful server side functionality. ASP is easy to use as it is designed around the object orientated programming model. ASP can be written in the two native scripting formats: Visual Basic and JScript (similar to JavaScript) and optional third party scripting engines can also be installed for Perl, Rexx & Python.

Basic knowledge in any scripting language will hold you in good stead with ASP. ASP is supported by Microsoft's IIS web servers and third party support is available for most Linux based servers i.e. Apache.

RULES: There are four basic rules for incorporating ASP into your web pages.

  • Files must be named with the .asp extension

  • The language you are using for the ASP page must be declared on the very first line of your HTML page.

  • The use of <%    %> delimiters are used to encapsulate any ASP code or directives.

  • The ASP page must be called or served from a web server i.e. You can not just open the page in your browser.

In all of these examples I have used VBSCRIPT as the default language. The reason: I consider VBSCRIPT to be less prone to human coding errors as less code is required over JSCRIPT. If you do wish to use JSCRIPT then code formatting rules are very similar to JAVASCRIPT in that the use of the ; to end a line of code is required and other formatting rules associated with JSCRIPT.

Example:
http://www.mywebsite.com/myaspfile.asp

<% @ LANGUAGE="VBSCRIPT" %>   ' Declare the coding language that is to be used
<html>
<head>
</head>
<body>
</body>
</html>

Comments:
Throughout these examples you will see me use either // or ' to give explanations on what the line of code is for. These are called comments and are not processed by the server. When writing any code it is wise to use comments to indicate what each particular piece of code is being used for. There is nothing worse than having to alter code that you worked on 12 months ago and having to spend some time trying to figure why you used this particular line / area of code or why it was written in the that manner. Comments can save you lots of time and frustration down the track.

Variables:
If you are already familiar with scripting languages then you will have no problems declaring & using variables in ASP other than the call / display procedures. If you are new to scripting then the concept behind variables are simply that they are used to hold information. Each variable you use is given an identifying name then the value that the variable is to hold. You can then access any variables value by referencing it's name.
E.g.
Dim myVariable 
myVariable = "This is the value of my variable"

The word "Dim" is used in VBSCRIPT to declare or inform the the server that a variable is being created. You do not have to immediately give a declared variable a value. A variable's value can be set or modified at any time throughout your code.

There are two types of variables that can be used: Global and Local. There are differences in their use and the way in which they will hold a value. I will not go into detail here on their separate uses other than to say that a Globally declared variable can be accessed at any time throughout your code and a Local variable may only be accessed / used in a specific area or procedure within your code. In all of these examples unless specified we are using Global variables.

Naming Conventions:
At times pages may end up with 100's of declared variables if your application is large. To help you remember what every variable is being used for you should name your variables in a descriptive manner. A rule of thumb is to use an uppercase character for the second part of your variable if required ie. myVariable or with an underscore my_variable.  Another well used option is to keep you informed on what type of value is being stored in your variable ( for value types see below ). If your variable is to hold a string or text then the first part of the name can be strMyVariable with the str informing you that the variable holds text or a string. For integers or numbers use intMyVariable.
Example: If you require a variable to hold a Grand Total Amount for a number of variables that are being added together you could use:
Dim int_grandTotal
.

Whichever naming convention you decide to use, keep it uniform across you site.

NOTE: If copying code directly from the page and not from the text box's then copy code and paste into notepad, copy from notepad into your HTML editor! If you do not do this then the code may be corrupt, depending on your HTML editor.

<%@LANGUAGE="VBSCRIPT"%>

<%
Dim variable_name    
' Declare a variable with the name "variable_name"
Dim variable_name2   
' Declare a variable with the name "variable_name2"
variable_name = "Vortex"     
' Assign variable_name with a value

variable_name2 = "Media"
%>
<html>
<head>
<title> My ASP Trial </title>
</head>
<body>
<p>
<font color="#800000" face="Arial" size="4">
You chose to use <%= variable_name %> <%= variable_name2 %> for your IT resources.
</font>
</p>
</body>
</html>

You will notice that we have encapsulated the ASP code with the <% & %> delimiters. The = part of the delimiter informs the ASP engine that the variable's value is to be written directly to the page.

Example:
Create a page with the code below, save & upload to your server then call the page.

 

 
   

Variable Values & Operators


The Values:

There are three main types of values that can be stored in a variable: strings or text, numbers or integers and arrays.
Text / string values are encapsulated by double quotes " ".  myVariable = "Vortex Media"
Numbers are not encapsulated i.e. no quotes ( myVariable = 54 )
Arrays are variables that can hold multiple values and are not covered in this section. To learn about array's See Below

Numbers or Integers:
While you can store numbers as text "54" if you intend to use mathematical equations on a variable's value it is wise to declare that value as an integer: 54 without quotes.

Operators & Documentation:
The full Windows Script Documentation can be downloaded (1.36Mb) free from Microsoft at the following link and is recommended if you are teaching yourself ASP.
Microsoft Script Library Download. To view all operators and their use: After install type in "operator" under the index tab.

 

 
   

Changing A Variables Value & Basic Mathematic Principles


Changing a value:
After declaring and consigning a value to a variable, the value can be easily changed.

<%

Dim myVariable
myVariable = "my initial value"      
' assign initial value
myVariable = "this is my changed value"     
' assign a new value to myVariable

%>

Simple Maths:
A variables value can be manipulated by using math.

Example 1:
<%

Dim int_myNumber
int_myNumber = 54 + 10        
'int_myNumber holds the value 64

%>

Example 2:
Variables may be added to each other to assign a value to another variable or back into itself
<%

Dim int_myNumber
Dim int_myNumber2
Dim int_myTotal

int_myNumber = 54
int_myNumber2 = 10
int_myTotal = int_myNumber + int_myNumber2

%>
<%= int_myTotal %>         
' prints the value stored in int_myTotal to the page: 64


Example 3:
Assigning a new value back into itself. Note the alternative way of declaring multiple variables and the use of parenthesis ( ). Parenthesis is used to ensure that an equation is performed in the correct order. In this example the use of the parenthesis ensures that the two supplied variables are added together before the last variable is subtracted.
<%

Dim int_myNumber, int_myNumber2

int_myNumber = 54
int_myNumber2 = 10

int_myNumber = (int_myNumber + int_myNumber) - int_myNumber2

%>

<%= int_myNumber %>            '  The value of int_myNumber is now 98: 54 + 54 - 10 = 98

Example 4:
The order in which mathematical equations are performed and the use of parenthesis has a big influence on the result. By default all code has an operator precedence (the order in which operators are evaluated) the use of parenthesis will override this order. Parenthesis is not required in all instances as the default order would have achieved the same outcome, but it is advised to use parenthesis to ensure correct order precedence.

Take this example for instance.
int_myNumber = 54 + 54 - 10     gives the result of 98
int_myNumber = (54 + 54) - 10   gives the result of 98
int_myNumber = 10 - 54 + 54     gives the result of 10
int_myNumber = 10 - (54 + 54)   gives the result of -98

 

 
   

Arrays


How They Work:

An array is simply a variable that can hold multiple values.
Example:

Dim myArray
myArray = Array("String1","String2","String3")

Referencing an array's elements:
Each item in the array is called an element, each element is numbered starting from 0: String1 is stored in element 0 of the array, String2 is stored in element 1 of the array and so on. Parenthesis is used to reference which element of the array you wish to access with the elements number included.

Example:
<%

Dim myArray
myArray = Array("String1","String2","String3")

%>

<%= myArray(0) %>
<br>
<%= myArray(1) %>
<br>
<%= myArray(2) %>
This would print to the page:
String1
String2
String3

 
   

QueryStrings & Form Data


The retrieval of QueryStrings and Form data is simple and easy to understand and can add great functionality to your site

The QueryString:
Two options in using QueryString are:

Retrieving a parameter passed in a URL. You will probably have noticed when browsing the Internet that you will often see a URL portrayed as "http://www.vortexmedia.com.au?myParameter = My ASP Test". The ? in this occasion holds  a variable/parameter called myParameter, the value that this variable holds is 'My ASP Test'.
All the information contained in the 'myVariable'  including the parameter's name and value are passed to the browser via the URL.

To retrieve this parameter and write it's value to the HTML page is as follows:
<%= Request.QueryString("myVariable") %>
You will notice that we have encapsulated the ASP code with the <% & %> delimiters. The = part of the delimiter informs the ASP engine that the variable's value is to be written directly to the page.

Example:
Create a page with the following name "myAsp1.asp" insert the code below, save or upload to your server then type the following URL in your browser to call the page:
http://www.yourwebsite.com/myASP1.asp?myVariable=MyASP TEST


Retrieving a parameter passed in a Form. (How to use forms click here)
This example can only be used when the form uses the "GET" method to send information: Using "POST" as the forms method is covered in the next section:

When a form uses "GET" as it's method, the forms field names and their values are passed to the browser the same way as using the URL in the previous example. To retrieve the field values is the same as above.

Form Example:
Create a page with the following code and name it "myForm1.htm" save or upload to your server and call the page. Then click submit to send the form's data to your "myASP1.asp" page.

Retrieving a parameter passed in a Form that uses POST:
This method is almost exactly the same as using "GET" other than you must replace the Request.QueryString("") with Request.Form(""). POST data is passed in the head of the URL and is not visible to the user.
<%= Request.Form("myVariable") %>

NOTE: Both of the Request methods above will retrieve data from a submitted form. If a Form Field has multiple values as an example when using drop down box's that can have multiple selections then all values for that field will be returned with a , (comma) seperating each value.
 

 
   

More Tutorials To Be Added Soon


As I have only just started these tutorials, more will be added when I am able to find the time. I am hoping to be able to add 2 per week but time restrictions will come into play. If you wish to request a tutorial then email me and I will respond to you with an indication of when or if I am able to add your request to the site.

Upcoming Tutorials:

  • Loops, arrays and iteration

  • Databases: Connections, How to read and write to a database, How to write a forms information to a database

  • How to email a form

  • Formatting and rounding of prices / currency
     

Email: support@vortexmedia.com.au
This page added 18 Dec 2002

 
   

Need Help With This Code


If you do find yourself having problems getting this code to work then please contact me at support@vortexmedia.com.au explaining the problems or errors you are having. I will try to get back to you quickly but please be aware that at times I am unable to reply to all requests in a speedy manner.

 

 

  Client Login
 

Disclaimer \ Privacy

© Vortex Media 2004
All Rights Reserved