How to use the QueryString in ASP
Published 2 years, 9 months ago in ASP and ASP BasicsThis article tells you how to pass variables to an ASP page using the QueryString, and how to access them from that page.
Have you ever seen a URL which looked like "www.example.com/page.asp?mode=1&style=red"? Well, this page is being passed variables and their values through the QueryString, here the variables "mode" and "style" are being passed, with values "1" and "red" respectively. The question mark indicates the start of the query string and the ampersand, &, symbol seperates variable=value assignments.
The page is then able to read these variables and react according to them, for example to display them to the user.
Passing a QueryString to a page
There are two ways to pass a QueryString to a page, the first is to enter it manually, for example:
-
<a href="page.asp?mode=1">Mode 1</a>
The above HTML creates a link to a page passing the variable mode with the value "1".
An alternative, and perhaps more useful, way is to use HTML forms. The main thing to remember with forms is that you need to use the GET method to send information via the QueryString, for example:
This code displays a simple HTML form with a text box and a submit button. When the user enters their name and presses submit, the information in the name box is passed to a page called "page.asp" (specified in the form's action attribute) via the QueryString. So, if I entered "mooseh" and pressed submit, the form will call page.asp?username=mooseh. Remember that "username" was what we entered as the text input's name attribute.
Accessing a QueryString element in ASP
In ASP, all the information passed via the QueryString is held in the Request.QueryString collection. To access an item, type Request.QueryString("varName"), where varName is the name of the variable in the QueryString.
To demonstrate, let's create page.asp to process the information from the form above:
-
<%@language="VBScript"%>
-
<% If Request.QueryString("username") "" Then
-
Response.Write "Hello, " & Request.QueryString("username")
-
Else
-
Response.Write "You did not enter a name."
-
End If
-
%>
So now, me typing in mooseh and pressing submit will generate the reply "Hello, mooseh".
Listing the contents of Request.QueryString
Should you wish to find out everything that's in the QueryString, you can do so with the following code which displays the variables next to their values in a table:
-
<%@language="VBScript"%>
-
<table border="1">
-
<tr><th>variable</th> <th>value</th></tr>
-
<%
-
Dim variable
-
For Each variable in Request.QueryString
-
Response.Write "<tr><td>" & variable & "</td>"
-
Response.Write "<td>" & Request.QueryString(variable) & "</td></tr>"
-
Next
-
%>
-
</table>
Conclusion
This article showed you how to pass information between pages with the QueryString, and a use which allows you to add some increased interactivity. There are a lot of other uses, for example selecting which section of a site to display based on a variable in the QueryString, and processing form data.
If you have a website, consider joining the Money4Banners advertising network. They will pay you £10 + £5 each month for displaying a small advert on three of your pages, regardless of traffic. American webmasters are welcome, and since £10 == $20, you make more!
Search
Categories
- ASP (3)
- ASP Basics (3)
- Blogs (2)
- C# (1)
- deals (1)
- Firefox (1)
- Flash (1)
- Google (6)
- Hacks (4)
- Java (1)
- PHP (12)
- Basics (8)
- Files (1)
- Image Handling (3)
- Puzzles (1)
- Ruby (1)
- Tips (10)
- Web Hosting (3)
- Web Hosting Articles (1)
- Web Hosting News (2)
- Windows (6)
- Wordpress (2)
hi
the information that u provided on this page is good but i need to ask one thing that what if the field that we are sending in the querystring contains an ampersand …..actually i facing this problem
eg
i want to send two names in the querystring and they are stored in the database as neetu & geetu….then it does not read the 2nd name ‘geetu’ …please can u help me in this regard …
waiting for your reply
charu
Hi charu, you need to encode the ampersand so that it gets sent properly through the QueryString.
You can do this by replacing the & in the query string by %26, eg page.asp?blah=one%26two - then blah will have the value one&two.
To have ASP encode a string automatically like this for you to use in the query string, you can use the Server.URLEncode method.
eg:
Server.URLEncode(”neetu & geetu”)
produces neetu+%26+geetu (since spaces are converted to + and & is converted to %26).
Hope this helps,
steve
it really helped me thanks
Is it possible to modify the form method dynamically the way we can modify the form action for instance. Like on the click of button1 the method is post and the action is URL1 but on the click of button2 the acyion is GET and the action is URL2?