dcsimg

Request.QueryString Collection

By ServerWatch Staff (Send Email)
Posted Jan 17, 1999


Sirgio Pereira

The Request.QueryString Collection is very similar to the Request.Form Collection and its formal definition is almost identical too:

The Request.QueryString Collection is very similar to the Request.Form Collection

Request.QueryString(element)[(index)|.Count]

When dealing with the Request object we are accessing information given or returned by the client. The QueryString access the data that follows up the URL. To start lets see a practical example. When starting to write my articles here to SWYNK.COM, I was faced with one dilemma: should I update my index page every time that an article is posted, or should I found an easy way of updating it automatically? I obviously choose the last one. Here's how. Almost every link on my default.asp page is composed in this way:

verifica.asp?pagina=<name of page here>

(Verifica means verify and pagina means page in English)
The verifica.asp script will then check if the page indicated by pagina exists and if so it will redirect the visitor to it or else it will redirect it to a construction-warning page. The code is as follows:

-- verifica.asp --

<%
Response.Expires=0
pagina=Request.QueryString("pagina")
path = Server.MapPath(pagina)
Set fs = Server.CreateObject("Scripting.FileSystemObject")
If Not fs.FileExists(path) Then
Response.Redirect "construcao.asp"
Else
Response.Redirect pagina
End If
%>

-- End of page --

Response.expires=0 tells proxies and browsers not to cache this page. The next line is the focus of this article. In it I retrieve the name of the page given by the link into variable pagina. Then I can use it to do the required processing. In particular I assign to path the real path to the file using Server.MapPath method and then test it existence using the Scripting.FileSystemObject. If the page exists (I've already posted the article) then Response.Redirect redirects the user to it. The ( not ready ) you can see on my index page is made in a similar manner.

Now back to Request.QueryString. This method of passing data can be used with two different objectives in mind, One is to make a flow control and the other is to pass data to a script from the client. We've seen the first and will now move on to the second. If you already readed my Request.Form article, you'll notice that there are two forms on posting a form.

<form name="form_name" action="script_page_or_cgi" method="post_or_get">

When given the value get to the attribute method, the browser will post all data to the server on the link request, rather than on http-headers. To access this information in ASP, you must use Request.QueryString collection in the same way of Request.Form. For more detailed information on how to do this, please read my article on using Request.Form, I've already covered most of the topics there. Now let's review it in more detail.

<form method="get">
Your name<input type="text" name="name"><br>
Your age<select name="age">
<option value="0">Under 18</option>
<option value="1">Between 18 and 23</option>
<option value="2">Between 24 and 45</option>
<option value="3">Older than 45</option>
</select>
</form>

This little Html form will be the basis to our discussions. Let's say we have a script that will accept our form. And we want to retrieve the information given by the user to variables. Then our code will be:

<%
name=Request.QueryString("name")
age_classe=Request.QueryString("age")

%><html>
<head>
<title>QueryString Example</title>
</head>
<body>
your name:<%=name%><br>
You are aged <%
Select Case age_classe
Case 0
Response.Write "under 18 years."
Case 1
Response.Write "between 19 and 23 years."
Case 2
Response.Write "between 24 and 45 years."
Case 3
Response.Write " more than 45 years."
End Select
%>
</body>
</html>

All the considerations taken into account with the Request.Form collection can be made too to the Request.QueryString.

 

I want your feedback! Mail me!

Page 1 of 1


Comment and Contribute

Your name/nickname

Your email

(Maximum characters: 1200). You have characters left.