BackUp LevelNext

Passing Variables to Pages with URLs and Forms

In some cases, you will want to make a parameter that exists in one page available to another page. There are many ways to pass a variable between two pages: in a URL, in a form, or using browser cookies and client variables.

Passing parameters with a URL

You can pass dynamic parameters from one page to another by appending them to the URL reference to the target page. Separate the parameter from the URL filename address with a question mark (?). Parameters are passed by appending the name of the variable you are passing and its associated value.

The scope of these variables is the target page of the hyperlink that carries the URL variables.

Example

In this example, the hyperlink passes a variable named "user_id" with a value of 5 and a variable named "color" with a value set to the expression #mycolor# to the example.cfm page:

<A HREF="example.cfm?user_id=5&color=#mycolor#">

In the target page, example.cfm, you can refer to these variables as URL.user_id and URL.color.

<CFOUTPUT>
Your user ID is #URL.user_id# and 
your favorite color is #URL.mycolor#.
</CFOUTPUT>

Formatting issues

When formatting URL parameter values, keep the following formatting issues in mind:

If you are passing values that may contain spaces or special characters, use the function URLEncodedFormat.

<CFSET FullName="Bob Smith">
<CFOUTPUT>
<A HREF="printname.cfm?FullName=#URLEncodedFormat(FullName)#">
Click here</A>
</CFOUTPUT>

See the CFML Language Reference for more information on the URLEncodedFormat function.

Passing parameters with a form

You can use a form to pass variables between pages by allowing a user to enter or choose a value in a form. Use the resulting Form variables on the form's action page to process values passed in the form. You can also use hidden input types to pass a variable in a form to another page.

Example: Hidden input

The form below includes a hidden input named "Customer_ID" that is passed to the example.cfm page:

<FORM ACTION="example.cfm" METHOD="Post">
        <INPUT TYPE="Hidden" 
            NAME="Customer_ID" VALUE="24">
        <INPUT TYPE="Submit" VALUE="Enter">
</FORM>

The example.cfm page will be passed the parameter Form.Customer_ID with the value set by the previous page.

Example: Dynamic parameters

It is also possible to pass a value based on a dynamic parameter, such as the result of a query, as illustrated below:

<FORM ACTION="example.cfm" METHOD="Post">

<CFOUTPUT QUERY="GetCustomer">
    <INPUT TYPE="Hidden" NAME="Customer_ID" 
        VALUE="#Customer_ID#">
</CFOUTPUT>

<INPUT TYPE="Submit" VALUE="Enter">
</FORM>

In the example.cfm page, you can refer to the variables created with this form as Form.Customer_ID.


BackUp LevelNext

allaire

AllaireDoc@allaire.com
Copyright © 1998, Allaire Corporation. All rights reserved.