Wednesday 30 November 2011

PASS 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 accoring to what the user wants to see. So how do we do this?

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


Home Page


Choose a product category to view our products:

Books
or
Movies



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 a little more complicated so take it slow.

The next step is to create your products.asp page and paste the following code:

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


Products Page


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

Thank you for contacting us. We have received your message and will send a reply to your email address, <% Response.Write strEmail %>, as soon as possible.




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 Displaying 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.

http://www.aspwebpro.com/tutorials/asp/passvariableswqs.asp

No comments:

Post a Comment