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 of Form posting & Request object data handling

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.

https://www.plus2net.com/file_name.asp?name=john&country=USA&age=10
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.


Dim var_get
For Each var_get in Request.Querystring
Response.Write var_get & " = " & Request.Querystring(var_get) & "<br>"
Next

Form Post method posting & 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 . 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

Dim my_var
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


For Each var_get in Request.Form
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.

Be the first to post comment on this article :

plus2net.com




Post your comments , suggestion , error , requirements etc here .




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2022 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer