Free Guides
Language Tutorials

ASP ( Active Server Pages )
Index

Form handling in Asp
Form and request object in ASP
Forms in web pages are very common. We can have
signup forms to login forms and many other types of forms in our web
pages. Through forms users submit data to the server or interact
with the site. This is the way server gets user entered information
for further processing. Through a login form when we submit our user
id and password, the data reaches the server and is checked against
stored user id and password for validation.
Form design is a front end job so html is used to generate different
form components to get formatted data from the user. Different form
components are used based on the type of data to be entered by the
user. For example if the user has to enter the state name of USA
then it is better to ask the user to select the state name from a
drop down list box (showing the list of states) than asking the user
to enter the state name in a input text box.
Once the user submits the form, data processing is done at server
side and here we will be using validation at server side scripting
language to handle the data. We will learn how to process the form
data using ASP ( VBScript ). Such jobs can be handled by other
scripting languages like PHP, Perl, JSP etc.
In the form html code we will specify to which page the data is to
be send by using action attribute and we will also specify
method to tell the form the way data is to be posted. There are two
methods to post the data, one is GET and the other one is
POST method. Here is the code for basic html form.
<form name=f1 method=post action=file_name.asp>
… form components
</form>
From the above code we can see the method is
specified as post and the action is pointing to file_name.asp. So
here we will receive the data of the form in file_name.asp page for
processing. To handle the form data in receiving page ASP request
object is used. Next we will learn how to use
Get method and request object to collect data in ASP
As we have seen in ASP form basics, there is an
Request object to handle or receive data of a form in the action
page ( page specified in form action attribute ). We will learn how
to get data using request object and how the data is posted through
query string in GET method.
In GET method data of the form is passed through query string
in name and value pairs. The data is separated from the file name by
question mark( ? ) and between each pare of name and value ampersand
( & ) symbol is used to separate. Here is the example how in the GET
method data is transferred.
You can see in the above query string the form
data is kept after the file name and the question mark ( ? ) . Then
name and value pairs are used and they are separated by ampersand (
& ). This is the way data is posted using URL or the address bar of
the browser window.
Collecting data using Request object in ASP
Once the form is submitted data is available to the page indicated
at the action attribute of the form. Here we have to use
Request.QueryString to get the value of the data field. To get the
name entered by the user in name field here is the code
Dim name
Name=Request.QueryString(name)
Here we declared name as a variable for in the
present script and then used Request object to collect the
data of the name field. Now name field data is available to the
variable name. Same way other data can be collected by using query
string.
Here is the code of a form
<form method=get action=my_file.asp>
Your Name<input type=text name=name><br>
Age (years)<input type=text name=age size=3>
<input type=submit value='Submit Data'>
</form>
Here is the asp code inside my_file.asp
file to collect the query string data
Dim name,age
name=Request.QueryString("name")
age=Request.QueryString("age")
Response.Write ("Welcome " & name & " You are " & age & " Years
Old")
We can get all the query string data displayed by
using for each loop as they are stored as name value pairs. Here is
the code. Note that we are not using any individual form component
name to collect the data.
For Each var_get in Request.Querystring
Response.Write var_get & " = " & Request.Querystring(var_get) & "<br>"
Next
Post method of form data handling by request object
In POST method of sending form data to processing page is more used than GET method. In POST method data entered inside the form is passed to action page by using HTTP header. The main difference visible here is that data is not passed through URL as a query string in user address bar (like GET method). Here is the form code for method object.
<form name=f1 method=post action=file_name.asp>
… form components
</form>
In POST method data can be collected by using request object of ASP. Here is the code
my_var=Request("city")
Here my_var is a declared variable and city is the form component name where city name is entered by the user. Now the value entered by the user is available at variable my_var
Like in the Get method we can also loop through and display all the name value pairs associated in POST method of form submission. Here is the code
Response.Write var_get & " = " & Request.Form(var_get) & "<br>"
Next
But this is not a good practice of allowing all variables to
initialize as this can be exploited to pass unwanted variables into
the script.
Collecting checked radio button value of a HTML form in ASP
Radio buttons in a form in ASP is used to collect
user's choice. Here radio button is used when user has to select one
of the many options presented. For example we want the visitor
should say the mode of transport he select while traveling. Here one
of the four options is to be decided by the visitor so we can use
radio button here. As a part of the form component radio buttons can
be used along with other components of a form. We will discuss here
only the handling of radio button in a form and collecting the radio
button value through ASP.
A group of radio buttons will have same name but different values.
In a form there can be any number of groups of radio buttons but
each group of buttons will have same name.
We will try here with one group of radio button giving the visitor
to select one choice but same can be extended to more than one group
of radio buttons. Here is the code to display a group of radio
buttons inside an html form.
<input type=radio name=t1 value='road'>Road
<input type=radio name=t1 value='rail'>Rail
<input type=radio name=t1 value='air'>Air
<input type=radio name=t1 value='ship'>Ship
<input type=submit value='Submit'>
</form>
As we submit the form the value of the selected
radio button will be available at the action page and we can collect
the value of this radio button like this.
Dim mode
mode=Request("t1")
Response.Write " Mode of Transport is " & mode
This way the selected mode of transport will be available for further processing.
Getting the checked values of checkbox Array of a form in ASP
Checkbox are shown to visitors to get the opinion
on a fixed value. Sometime the visitors may have to submit more than
one choice out of a group of options so here checkbox can be shown
to the visitors. For example we can ask the visitor to submit what
are the games they play and we can show a list of games. Visitor may
select one or more ( or may select all ) games by clicking the
checkboxes. Here choice can be more than one ( In case any one to be
selected out of the list then radio button is to be used ) so
checkbox is the best option. We will learn how to collect the
checked values of the form checkbox inside ASP for further
processing. Here is the html form with checkboxes.
<input type=checkbox name=t1 value='cricket'>Cricket
<input type=checkbox name=t1 value='football'>Football
<input type=checkbox name=t1 value='tennis'>Tennis
<input type=checkbox name=t1 value='hockey'>Hockey
<input type=submit value='Submit'>
</form>
Here we have used t1 as common name for all
the checkbox with different values so they will be treated as a
group of checkbox. Once the form is submitted then the values of the
checked checkboxes will be available to the action page and can be
collected like this.
mode=Request("t1")
We have used t1 as the common name so all
the values of the checkbox for which the visitor has checked will be
available as Request("t1") as comma separated values. So in
the above code the variable mode value will be consisting of
all checked values separated by comma.
So now let us split the string to get the array of checked values.
Now we will loop through the array by using for
for loop and UBound function to print out the checked values of the
checkboxes.
Response.Write mode_a(i) + "<br>"
Next