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

Course Booking

  Course ID:  
  Course Name:  
  Start Date:  
  End Date:  
    Your Details  
  Name:  
  E-Mail:  
  Phone:  
  Mobile:  
 
     

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...

Code:

<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 an ASP function as the name of the course contains a space so it has to encoded for the URL so we use <%=Server.URLEncode("ASP Biginners")%>, this uses the ASP function 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.

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