Lesson 10: Passing variables in a URL

When you work with ASP, you often need to pass variables from one page to another. This lesson is about passing variables in a URL.

How does it work?

Maybe you have wondered why some URLs looks something like this:


	http://html.net/page.asp?id=1254
	
	

Why is there a question mark after the page name?

The answer is that the characters after the question mark are a HTTP query string. A HTTP query string can contain both variables and their values. In the example above, the HTTP query string contains a variable named id, with the value 1254.

Here is another example:

	http://html.net/page.asp?navn=Joe
	
	

Again, you have a variable (name) with a value (Joe).

How to get the variable with ASP?

Let's say you have an ASP page named people.asp. Now you call this page using the following URL:


	people.asp?name=Joe
	
	

With ASP you will be able to get the value of variable 'name' like this:

	Request.QueryString("name")
	
	

So, you use the object DocumentationRequest and DocumentationQueryString to find the value of a named variable. Let's try it in an example:


	<html>
	<head>
	<title>QueryString</title>
	</head>
	<body>
	<%

	' The value of the variable is found

	Response.Write "<h1>Hello " & Request.QueryString("name") & "</h1>"
	%>
	</body>

	</html>

	
	

When you look at the example above, try to replace the name "Joe" with your own name in the URL and then call the document again! Quite nice, eh?

Several variables in the same URL

You are not limited to pass only one variable in a URL. By separating the variables with &, multiple variables can be passed:


	people.asp?name=Joe&age=24
	
	

This URL contains two variables: name and age. In the same way as above, you can get the variables like this:

	Request.QueryString("name")
	Request.QueryString("age")
	
	

Let's add the extra variable to the example:


	<html>
	<head>
	<title>QueryString </title>
	</head>
	<body>
	<%
	' The value of the variable name is found

	Response.Write "<h1>Hello" & Request.QueryString("name") & "</h1>"

	' The value of the variable age is found

	Response.Write "<h1>You are " & Request.QueryString("age") & " years old</h1>"

	%>

	</body>
	</html>
	
	

Now you have learned one way to pass values between pages by using the URL. In the next lesson, we'll look at another method: forms.



<< Lesson 9: Functions

Lesson 11: Passing form variables >>