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 URL="file.cfm">
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">
Emplist.cfm after the update is complete.During the next procedure, use CFLOCATION on your action page to redirect users to Emplist.cfm.
| 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. |
| To redirect users to another URL: |
ActionPage.cfm in HomeSite.ActionPageRedirect.cfm. 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.
EmpList.cfm page:<CFLOCATION URL="EmpList.cfm">
The CFLOCATION tag will now immediately redirect users to another page.
FormPage.cfm in HomeSite.FormPageRedirect.cfm.ActionPageRedirect.cfm.FormPageRedirect.cfm in a browser.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: | 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.