Passing Variables with QueryString

Ok, so you want to pass variables between your web pages with the using the QueryString method or the URL bar for you folks still learning ASP lingo.  Well, it’s as easy as it is passing variables using the form method.  One of the most simple and popular ways of passing variables using the QueryString method is by using a basic hyperlink.

Let’s keep things simple and say that you sell books and movies on your website.  Rather than creating a books.asp page and a movies.asp page to list your available products, you can put all your products into a database and display them all on one page according to what the user wants to see.  So how do we do this?

The first thing you need to do is create your home /Index.asp page and paste the code below into your page:

Choose a product category to view our products:

<a href="products.asp?Products=Books">Books</a>
or
<a href="products.asp?Products=Movies">Movies</a>

 

Notice that both links point to the same /Products.asp page.  However, they each have a variable attached to the link with a different value.  When you click on the Books link, you will see the variable and its respective value displayed in your URL bar like this: http://www.yoursite.com/products.asp?Products=Books

Now, how do get this to display your books on your products.asp page?  It’s simple.  Next, create your /Products.asp page and paste the following code:

<%
DIM strProducts
strProducts = Request.QueryString("Products")

IF strProducts = "Books" THEN
' Display books here
ELSE
' Display movies here
END IF
%>

 

That’s all there is to it.  Now, you can pass variables from one page to another using the QueryString method.  If you are eager to learn more about how to actually display data from your database on your page, be sure to check out our Display Data From Database tutorial.

If you want to learn another way to pass variables between pages, be sure to check out our Passing Variables with Form tutorial.

< Back to ASP Tutorials