iNET Interactive - Online Advertising Agency
          
   Home    Authors    About    Login    Contact Us
   Search:   
Advanced Search     
  Articles

  ASP (26)
  ASP.NET (19)
  C and C++ (4)
  CFML (2)
  CGI and Perl (16)
  Flash (2)
  Java (7)
  JavaScript (28)
  PHP (92)
  MySQL (13)
  MSSQL (3)
  HTML (34)
  SEO (9)
  Visual Basic (12)
  CSS (13)
  SSI (5)
  XML (12)
  C# (14)

  Developer News

May 11, 2008
Improving accessibility for motor impaired users
WebDevTips UK
 
May 11, 2008
10 ways to orientate users on your site
WebDevTips UK
 
May 11, 2008
Web Design Clinic - Rros restoration camp 2006
About
 
May 10, 2008
The Moods of Facebook
About
 
May 9, 2008
CSS 1 properties are a great start
About
 
May 9, 2008
Reader Question: How do you get fancy fonts?
About
 
Courtesy of moreover.com
 
Want to receive new articles via e-mail? Click here!
/Home /ASP

Using the ASP QueryString 

  Views:    35809
  Votes:    10
by Andrew Schools 4/13/04 Rating: 

Synopsis:

In this tutorial, we will discuss the benefits of using the QueryString and how to use the QueryString.
Pages: 
The Article

What's the QueryString

When you visit a site, you may sometimes see a url like this:

http://www.codehungry.com/index.asp?id=12345&name=codehungry

What's all this? Well, before the ?, it's just a plain old url, however, everything after the ? is considered the QueryString.

So what's so special about the QueryString? It's very powerful and of course, very useful. For instance, if you wanted to get a certain file from a database, you would give the user a link to click:

content.asp

<a href="http://www.codehungry.com/index.asp?id=8">File 8</a>

Once the user clicked on the link, they would of course be directed to http://www.codehungry.com/index.asp?id=8. To get the querystring id, you would do the following:

Request.QueryString("id")

If you wanted to write the value of the querystring id, you would do the following:

Response.Write Request.QueryString("id")

Which would give you a value of 8.

Now in order to make the QueryString useful, we need to create a script that will read through the database looking for the id that's = 8. If you do not understand this, then you should read the tutorial Simple Database Techniques.

index.asp?id=8

<!--#include virtual="adovbs.inc" -->


<%


Dim DB, RS


Set DB = Server.CreateObject("adodb.connection")


DB.Mode = adModeReadWrite


DB.Provider = "MICROSOFT.JET.OLEDB.4.0"


DB.ConnectionString = Server.MapPath("db1.mdb")


DB.Open


Set RS = Server.CreateObject("adoDB.recordset")


RS.Open "SELECT * FROM table1 WHERE id= " & Request.QueryString("id"), DB, adOpenKeySet,adLockOptimistic,adCmdText

If RS.BOF and RS.EOF Then

Response.Write "<p align='center'>Sorry, no files!</p>"

Else


Response.Write RS("file_description")

End If

RS.Close

Set RS = Nothing

DB.Close

Set DB = Nothing


%>

The following code will obtain a file from the database with it's id = 8.

So, if you wanted to create an content page, create a list of links like so:

content.asp

<a href="http://www.codehungry.com/index.asp?id=1">File 1 </a> <BR>

<a href="http://www.codehungry.com/index.asp?id=2">File 2 </a> <BR>

<a href="http://www.codehungry.com/index.asp?id=3">File 3 </a> <BR>

<a href="http://www.codehungry.com/index.asp?id=4">File 4 </a> <BR>

<a href="http://www.codehungry.com/index.asp?id=5">File 5 </a> <BR>

<a href="http://www.codehungry.com/index.asp?id=6">File 6 </a> <BR>

When the user clicks on one of the links, they will be taken to index.asp, which is a processing page that processes the QueryString. It then takes the value of the QueryString id and finds the correct file in the database.

Using more than one QueryString variable

What if you want to use more than one QueryString variable in your QueryString? You need to separate them by a &. For instance:

http://www.codehungry.com/index.asp?id=8&name=codehungry

To retrieve these values, do the following:

Request.QueryString("id")

Request.QueryString("name")

When using spaces in your QueryString or special characters like the ' character, you must use the URLEncode method of the Server object:

text = "Andrew's WebSite!"

Response.Write "<a href='index.asp?id=" & Server.UrlEncode(text) & "'>Click Here!</a>"

You should get the following QueryString:

index.asp?id=Andrew%27s+WebSite%21

To retrieve this sentence from the QueryString, you would do what you always have:

Request.QueryString("id")

 

Pages: 

Similar/related articles:


 
  Sponsors