Passing Parameters Example
This example has a table of courses with a link to book a course form. The table in this example is static and would be manually updated by a course administrator but it could easily be a dynamic table extracting the courses from a database.
Once a user decides which course they want to attend they click the link to book the course. The link passes the information through the URL to the booking form and uses the parameters to completes some of the details in the form, the user completes the remaining fields and clicks the book course button to submit the data.
The main purpose of this example is to show how information can be passed through a hyperlink to another page, which could be used in many situations.
Course Booking Example
Courses
Available Courses
Course ID Course Name Course Description Start Date End Date ASP 101 ASP Beginners A basic course for ASP coding August 11th 2008 August 15th 2008 Book Course HTML 01 HTML for Beginners Creating your first web page August 18th 2008 August 22nd 2008 Book Course
Code
In this example the important piece of code is the hyperlink, the rest is just a basic table, a form and the final results page.
The code for the hyperlink is as follows...
<a href='passparam.asp?id=ASP101&name=<%=Server.URLEncode("ASP Beginners")%>&start=11/08/2008&end=15/08/2008'>Book Course</a>
It is in most respects a normal hyperlink you would use to navigate to another page, the main difference is in the parameters added to the file name of the page.
The first parameter always follows directly on the end of the file name and is preceded by a question mark ? followed immediately by the parameter name, in this case id so the initial part of the hyperlink is passparam.asp?id.
Parameter names are immediately followed by the equals sign = then the parameter value again with no spacing. So in this case the value of the id parameter is ASO101, so we now have passparam.asp?id=ASP101.
Subsequent parameters are separated by the ampersand symbol & so again with no spaces we add the &name= to the hyperlink, where name is the next parameter.
On this occasion we need to use a VBScript function as the name of the course contains a space so it has to be encoded for the URL so we use <%=Server.URLEncode("ASP Biginners")%>, this uses the VBScript function (server.URLEncode) to encode the name of the course so it can be passed through the URL and will look like ASP%20Beginners in the URL.
Finally we add the two dates start and end, so again the ampersand & separates the parameters with no spaces followed by the parameter name the equals = and the value &start=dd/mm/yyyy and immediately the end date parameter &end=dd/mm/yyyy.
At this point we close the quotes around the href value and close the <a tag with >, and then finish our hyperlink with the text to be displayed "Book Course" And then close the hyperlink tag completely with </a>.
When the page called by the hyperlink opens the ASP code in the page can then read the parameter sent through the URL with Request.QueryString("Parameter Name") so to add the course id value to the input box on the form we use the following code.
<input type="text" name="ID" size="20" value='<%=request.querystring("id")%>' />
This is a standard text input box with the value assigned from the Request.QueryString("id") parameter
NOTE: parameter names passed through the query string are case sensitive so ID is different from id, I always make a habit of using lowercase parameter names to avoid confusion.
Passing Parameters Comments
Please feel free to leave comments, questions or suggestions here, I will respond to questions as soon as possible but please leave your correct email address as I sometimes email responses to questions.
Add Comment
Hi Richard
Yes just pass the recordset data as you would anywhere else in a page...
<a href="page.asp?id=<%=rs("id")%>&mode=<%=variable%>">Link</a>
In the line above i pass one value from a recordset and one from a variable.
Hope this helps
Regards
Ian
What if i wanted to pass parameters that come from a database, can that be done in the same way?