Get method and request object to collect data in ASPAs 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
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>
Here is the asp code inside my_file.asp file to collect the query string data
Your Name<input type=text name=name><br>
Age (years)<input type=text name=age size=3>
<input type=submit value='Submit Data'>
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.
Response.Write ("Welcome " & name & " You are " & age & " Years Old")
For Each var_get in Request.Querystring
Response.Write var_get & " = " & Request.Querystring(var_get) & "<br>"