Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 202,528 times

Contents

Related Categories

Beginning Active Server Pages - Query Strings

Query Strings

There will be times when you don't want the user to have to submit a form in order to get information. Take for instance, this site. We have a single ASP page - page.asp, which displays every article or code page on this site. But you don't have to enter an ID of the article in order to display it! Instead, the site uses Query Strings. The Query String is a bit of information added to the end of a URL, and takes one of 2 forms:

http://www.mysite.com/mypage.asp?this+is+some+text

and

http://www.mysite.com/mypage.asp?data1=value&data2=another+value

The latter is the most useful and most widely used.

To access the information sent in the query string, you use the Request.QueryString property... what a surprise! If, for example, you accessed your page with the following url:

/mypage.asp?id=4&title=Example+Title

In your ASP page, you could access this information by using Request.QueryString("id") and Request.QueryString("title"). Take a look at the following example:

<%
Response.Write "ID: " & Request.QueryString("id")
Response.Write " Title: " & Request.QueryString("title")
%>

Save this to a file called querystring.asp, and view it. At the moment, you'll see

ID: Title:

This is because no data has been sent in the Query String. Now try adding ?id=876. You will get

ID: 876 Title:

and finally, add ?id=876&title=My+Title, and you will see

ID: 876 Title: My Title

Query Strings can be especially useful when you need to retreive information from a database, but need a category ID, for example. Then, you can simply use

Set rData = cConn.Execute ("SELECT * FROM Categories WHERE ID=" & Request.QueryString("id"))

You will, by now have noticed some different characters in the query string. For example, + replaces a space. However, when you come to access Request.QueryString it is automatically removed again. If you want to convert a string to a query string, use Server.URLEncode. For example,

Server.URLEncode("my text & something ?")

becomes

my+text+%26+something+%3F

You can even make your forms generate a query string, so that instead of 'posting' its data, it generates a querystring instead. To do this, simply remove the method="POST" text from the form tag.

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments

  • Re: [1010] Beginning Active Server Pages - absolute beginner!

    Posted by michael poxon on 16 Dec 2006

        Hi,
    When I say I'm an ASP beginner, I mean it! I've only written one trivial bit of code, and got a blank page. I see now that's described in the article snippet below. I did ind...

  • .I want to know how to save the files.whats the extensions...

    Posted by writetoksk on 04 Oct 2006

    hi,


    its first i started ASP.I want to know how to save the files.whats the extensions...Where can i get the sample programs

  • Posted by James Crowley on 28 Dec 2004

    Are you running IIS ? And are you viewing it in your browser via the correct URL? (ie something starting with http:// rather than file:// ) ?

  • Posted by James Crowley on 28 Dec 2004

    It does - you just can't see it ;) We've got an ISAPI filter that rewritse /show/1010/ to something like /show.aspx?id=1010

  • It's not working ;_;

    Posted by HyperHacker on 12 Dec 2004

    Is there something special I have to do besides saving it as a .asp file, or does it just not work on my server? It just spits out the code, even HTML, as plain text.

    [code]
    Let's see if ASP...