Redirecting Users

When a user submits a form that adds or updates records, the action page should automatically go to a page that lists records , including the new or updated records.

Use the CFLOCATION tag to automatically redirect users from the current page to a different URL.

CFLOCATION Syntax

<CFLOCATION URL="file.cfm">

Usage example

In this example, the action page updates the CFExpress database and redirects the user to the EmpList.cfm page.

<CFQUERY NAME="UpdateEmployee" DATASOURCE="HRApp">
    UPDATE Employees
    SET FirstName = '#Form.FirstName#',
    LastName = '#Form.LastName#',
    Department_ID = #Form.Department_ID#,
    StartDate = #Form.StartDate#,
    Salary = #Form.Salary#,
    Contract= '#Local.Contract#'
    WHERE Employee_ID = #Form.Employee_ID#
</CFQUERY>
<CFLOCATION URL="EmpList.cfm">

During the next procedure, use CFLOCATION on your action page to redirect users to Emplist.cfm.

Note Note:This guide covers just enough SQL to get you going with ColdFusion Express. Refer to Chapter 9, Building Search Interfaces to learn about WHERE clause basics and refer to Chapter 10, Building Web Front-Ends to learn about the UPDATE statement.
Note To redirect users to another URL:
  1. Open ActionPage.cfm in HomeSite.
  2. Save this file as ActionPageRedirect.cfm.
  3. Delete all of the current output that is displayed to the user.

    During the next two chapters, you will learn how to use conditional logic and redirection to return data from, insert data into, and update data in a database.

  4. Add this code to the page to redirect users to the EmpList.cfm page:
  5. <CFLOCATION URL="EmpList.cfm">
    

    The CFLOCATION tag will now immediately redirect users to another page.

  6. Save the page.
  7. Open FormPage.cfm in HomeSite.
  8. Save the page as FormPageRedirect.cfm.
  9. Modify the ACTION attribute of the opening FORM tag to reference ActionPageRedirect.cfm.
  10. View FormPageRedirect.cfm in a browser.
  11. Submit the form.
  12. You are redirected to EmpList.cfm.

    If this was a form used to insert records into a database, you would have coded your action page to insert data within a CFQUERY tag and redirected users to this page.

Click here to see what results you should get.

Click here to see the new code on the action page.

Move on in this chapter to learn how to reuse the same code across your applications.

Note Note:Remember that the form passes values to the action page that's named in the opening FORM tag.

If you don't name the new action page in the ACTION attribute, it will not find ActionPageRedirect.cfm.