Passing Variables Between ASP Pages
There are many times when you will need to pass information from one ASP
page to another. Often times you are collecting user input, then passing
it onto an ASP page which will update or create a row in a database.
You can use HTML FORMs to send data easily from ASP page to ASP page.
There are two ways of accomplishing this: you can use a FORM with a
METHOD="GET" or a METHOD="POST". The METHOD of a FORM relates to how the
data will be passed. If you choose GET, the data is passed through the
QueryString, which means the data is passed in the string containing the
URL.
No doubt you have been to site where you've entered some data and the next
page has some URL which looks like:
http://www.someserver.com/scripts/myfile.asp?search=ASP+technology&boolean=AND
The words following the question mark are known as the QueryString. Let's
say you created a form in HTML which looked like this:
<FORM METHOD="GET" ACTION="/scripts/myfile.asp">
Search Terms:
<INPUT TYPE=TEXT NAME=search SIZE=50>
<P>
<INPUT TYPE=SUBMIT>
</FORM>
The user will see a text box. If they type in the word "Hello" and click
on the Submit button, they will be taken to the file supplied in the
ACTION keyword for the FORM. (If no ACTION is provided the current page
will simply reload.)
Since we used a GET, all of the information will be in the QueryString.
The QueryString, assuming I entered Hello in the textbox, will look
exactly like:
http://www.myserver.com/scripts/myfile.asp?search=Hello
If you specify your FORM as a POST, the data is still passed to the page
supplied in the ACTION, but it is not sent through the QueryString. If
you expect your users to be entering long, complex strings, or several (10
plus) data fields, it is usually wise to send the data through a POST form
as opposed to a GET form.
Once you have sent the data, you need to read it. In the file that was
specified by the ACTION tag, you can read the values into ASP variables
quite easily.
All you need to do is use the Request object. It is important that you
know the name of the FORM variable you are expecting. The name is denoted
by the NAME field in the INPUT tag. If you'll review the code example
above, we named the INPUT textbox "search". To read the value from search
into a variable, all we have to do is write code like this:
<%@ LANGUAGE="VBSCRIPT" %>
<%
'Create a variable named SearchText
Dim SearchText
'Read the search form variable. We will use CStr
'to make sure we are getting a string.
SearchText = CStr(Request("search"))
'Print out the value of SearchString
Response.Write(SearchText)
%>
It's that simple to pass data from one page to another. Just the use of
FORMs and use of the Request object inherent in ASP.
Happy Programming!