-Log in
-Register (free)


-RSS Feeds
-New MyDesk Module
-Update to Profile

Alternate color for RS looping with For Next
Northwind / ASP - Nested select
ASP SQL Injection




 Lesson 16: Query strings, part II
Lesson 16: Query strings, part II

Author: Alexander Haneng
Difficulty: Easy
Requires: ASP
Demo: n/a
Download: n/a
Summary:
Learn to use query strings to pass information between ASP pages. If you haven't read lesson 15, I would recommend reading it before this one.

Query strings with multiple parameters and values
In lesson 15 we learned to pass a single name and value pair, but you can create a query string with multiple parameters. To pass multiple parameters, simply join them with and & sign. If we want to pass e.g. an ID number as well as the visitor's name to the next page, we can use this link:
<A HREF="page.asp?id=10&name=Bob">Click here!</A>
To retrieve both the values on the next page we use this code:
<%
ID = Request.QueryString("id")
Name = Request.QueryString("name")
%>

You can also assign multiple values to one parameter. Like this:
<A HREF="page.asp?id=10&id=Bob">Click here!</A>
To retrieve this information we can use the Count property to count the number of values of the parameter. To count and display the number of values in the id parameter, we can use this code:
The ID parameter has <%=Request.QueryString("id").Count%> values.
To display these values, we can use a FOR EACH loop:
<%
FOR EACH id_value IN Request.QueryString("ID")
  Response.Write(id_value & "<BR>")
NEXT
%>

Dumping the QueryString collection
If you want to retrieve all the parameters in a query string, you can dump out all the info with a FOR EACH loop:
<%
FOR EACH QueryString_Parameter IN Request.QueryString
  Response.Write(QueryString_Parameter & "=")
  Response.Write(Request.QueryString(QueryString_Parameter) & "<BR>")
NEXT
%>

You can also use a FOR NEXT loop to accomplish this:
<%
FOR i=1 TO Request.QueryString.Count
  Response.Write(Request.QueryString(i) & "<BR>")
NEXT
%>

Or you can just dump it out raw:
<%=Request.QueryString%>

When to use query strings and when not to.
Query strings are best at sending small pieces of information between pages. Remember also that query strings are not hidden in any way, so it's not a good idea to pass sensitive data around with it, (like credit card information or passwords). For larger amounts of data, consider using forms instead.

Where to go next:
Check out the other lessons about parsing values between ASP pages: Lesson 8: Cookies and Lesson 13: Forms.


15: Query strings, part I
17: Access
| Info |
© Copyright 1997-2013 Alexander Haneng