Passing URL Variables

Pass the user's selection to a new page within an HTML anchor tag. When you do this:

When working with URL variables:

Passing URL syntax

<A HREF="URL?URLVariable">LinkName</A>

Passing URL variable examples

To create a hyperlink list that allows users to select an employee to update, you would enter this code:

<A HREF="UpdateForm.cfm?Employee_id=18">Jeremy Allaire</A>
<A HREF="UpdateForm.cfm?Employee_id=19">John Allaire</A>
<A HREF="UpdateForm.cfm?Employee_id=20">Marcello Fabiano</A>

To generate a hyperlink list so that users can select an employee to update, you enter this code on the listing page:

<CFQUERY NAME="GetEmployees" DATASOURCE="HRApp">
    SELECT     FirstName, LastName, Employee_ID
    FROM     Employees
</CFQUERY>

<CFOUTPUT QUERY="GetEmployees">
    <A HREF="UpdateForm.cfm?Employee_ID=#GetEmployees.Employee_ID#">
    #GetEmployees.FirstName# #GetEmployees.LastName#</A><BR>
</CFOUTPUT>

Using URL variables examples

This update form's query code retrieves database information by referencing the URL variable passed from the hyperlink listing page:

<CFQUERY NAME="GetEmployeeDetails" DATASOURCE="HRApp">
    SELECT     FirstName, LastName,
    Department_ID, StartDate,
    Salary, Contract
    FROM     Employees    
    WHERE  Employee_ID = #URL.Employee_ID#
</CFQUERY>

This code prefills the employee first name field using the VALUE attribute and dataset values:

<INPUT TYPE="Text" NAME="FirstName" SIZE="20" MAXLENGTH="50" 
VALUE="<CFOUTPUT>#GetEmployeeDetails.FirstName#</CFOUTPUT>">

This code prefills the department select box using conditional logic statements inside the OPTION tag:

Department<BR>
<SELECT NAME="Department_ID">
<CFOUTPUT QUERY="GetDepartments">
    <OPTION VALUE="#Department_ID#" 
        <CFIF GetEmployeeDetails.Department_ID IS     
            GetDepartments.Department_ID>
            SELECTED
        </CFIF>>
        #Department_Name#
    </OPTION>
</CFOUTPUT>
</SELECT>

This code prefills the contract checkbox using conditional logic inside the INPUT tag:

<INPUT TYPE="Checkbox" NAME="Contract" VALUE="Yes" 
    <CFIF GetEmployeeDetails.Contract IS "Yes">CHECKED
    </CFIF>>Yes
Note To build the hyperlink list to pass URL parameters:
  1. Create a new application page in HomeSite.
  2. Save the page as UpdateList.cfm.
  3. Title the page Chapter 10 - Employee Update List.
  4. Create a page heading after the opening BODY tag:
  5. <H4>Employee Update Listing</H4> 
    
  6. Query the Employees table and retrieve FirstName, LastName, and Employee_ID:
  7. <CFQUERY NAME="GetEmployees" DATASOURCE="HRExpress">
        SELECT FirstName, LastName, Employee_ID
        FROM Employees
    </CFQUERY>
    
  8. Add another heading to instruct the user to select an employee:
  9. <H4>Select an Employee to Update</H4>
    
  10. Create a CFOUTPUT block that references the GetEmployees query:
  11. <CFOUTPUT QUERY="GetEmployees">
    </CFOUTPUT>
    
  12. Insert an anchor tag within the CFOUTPUT block so that GetEmployees.FirstName and GetEmployees.LastName will appear as a hyperlink for each employee:
  13. <CFOUTPUT QUERY="GetEmployees">
        <A HREF=>#GetEmployees.FirstName# #GetEmployees.LastName#</A>
    </CFOUTPUT>
    
  14. Assign an HREF attribute to the anchor tag so that when a user selects an employee, the hyperlink passes the Employee_ID variable to the UpdateForm.cfm page:
  15. <A HREF="UpdateForm.cfm?Employee_ID=
    #GetEmployees.Employee_ID#">
    
  16. Format the hyperlink list by adding a BR tag after the anchor tag.
  17. Save the page.

Click here to see UpdateList.cfm's code.

Move on to the next procedure and create the update form.

Note To create the update form:
  1. Open InsertForm.cfm in HomeSite.
  2. Save the form as UpdateForm.cfm.
  3. Title the page Chapter 10 - Update Employee Form.
  4. Change the page heading to Update Employee Form:
  5. <H4>Update Employee Form</H4> 
    
  6. After the opening BODY tag, create a query named GetEmployeeDetails to retrieve data from the Employees table based on the URL parameter that will pass from the UpdateList.cfm page:
  7. <CFQUERY NAME="GetEmployeeDetails" DATASOURCE="HRExpress">
        SELECT FirstName, LastName,
        Department_ID, StartDate, Salary,Contract
        FROM Employees
        WHERE Employee_ID=#URL.Employee_ID#
    </CFQUERY >
    
  8. Change the FORM tag ACTION attribute to UpdateAction.cfm:
  9. <FORM ACTION="UPDATEACTION.CFM" METHOD="POST">
    
  10. Add a hidden form control to pass the URL.Employee_ID to UpdateForm.cfm:
  11. <INPUT TYPE="HIDDEN" NAME="Employee_ID" VALUE="<CFOUTPUT> 
    #URL.Employee_ID# </CFOUTPUT>">
    
  12. Rename the submit button Update Employee:
  13. <INPUT TYPE="Submit" NAME="SubmitButton" VALUE="Update Employee">
    
  14. Save the form.

Remain in the form and move on to the next procedure to prefill update form fields.

Note To prefill update form fields:
  1. Modify the FirstName and LastName fields to populate them with GetEmployeesDetails query data:
  2. <INPUT TYPE="Text" NAME="FirstName" SIZE="20" MAXLENGTH="50"  
    VALUE="<CFOUTPUT>#GetEmployeeDetails.FirstName# </CFOUTPUT>">
    <INPUT TYPE="Text" NAME="LastName" SIZE="20" MAXLENGTH="50" 
    VALUE="<CFOUTPUT>#GetEmployeeDetails.LastName# </CFOUTPUT>">
    
  3. Modify the select box so that the selected Department_Name is based on the GetEmployeeDetails query:
  4. <SELECT NAME="Department_ID">
    <CFOUTPUT QUERY="GetDepartments">
        <OPTION VALUE="#Department_ID#" 
            <CFIF GetEmployeeDetails.Department_ID IS     
            GetDepartments.Department_ID> 
            SELECTED 
            </CFIF>> 
        #Department_Name# 
        </OPTION>
    </CFOUTPUT>
    </SELECT>
    
  5. Modify the StartDate and Salary fields to populate them with GetEmployeeDetails query data:
  6. <INPUT TYPE="Text" NAME="StartDate" SIZE="16" MAXLENGTH="16"  VALUE="
    <CFOUTPUT>#GetEmployeeDetails.StartDate#</CFOUTPUT>">
    <INPUT TYPE="Text" NAME="Salary" SIZE="10" MAXLENGTH="10" VALUE="
    <CFOUTPUT>#GetEmployeeDetails.Salary#</CFOUTPUT>">
    
  7. Enable the checkbox based on GetEmployeeDetails query data:
  8. <INPUT TYPE="Checkbox" NAME="Contract" VALUE="Yes" <CFIF 
    GetEmployeeDetails.Contract IS "Yes">CHECKED
    </CFIF>>
    
  9. Save the page.

Click here to see UpdateForm.cfm's code.

Move on to the next procedure to create an update action page.