Making direct links to the forms

an article added by: David Smith at 03222008



In: Categories » Computers and technology » Programming » Making direct links to the forms

Say that you want to modify or delete a company. Right now, you need to remember the company’s ID, go back to the launch pad, click Company Edit, and enter the company ID, all just to get to the edit form. Wouldn’t you rather click a company in the list and go directly to the edit form?

...
   <table>
   <tr>
   <td><b>ID</b></td>
   <td><b>Name</b></td>
   <td><b>Address</b></td>
   <td><b>City</b></td>
   <td><b>State</b></td>
   <td><b>ZIP Code</b></td>
   <td>&nbsp;</td>
   </tr>
   <cfoutput query=”GetCompanies”>
   <tr>
   <td>#CompanyID#</td>
   <td>#CompanyName#</td>
   <td>#Address#</td>
   <td>#City#</td>
   <td>#State#</td>
   <td>#ZipCode#</td>
   <td>
   <a href=”EmployeeList.cfm?CompanyID=#CompanyID#”>Employees</a>
   <a href=”CompanyAddForm.cfm”>Add</a>
   <a href=”CompanyEditForm.cfm?CompanyID=#CompanyID#”>Edit</a>
   <a
   href=”CompanyDeleteForm.cfm?CompanyID=#CompanyID#”>Delete</a>
   </td>
   </tr>
   </cfoutput>
   </table>
 ...
  

Now you can go to the company list and click a link to go directly to the Add, Edit, or Delete Form. Now that you’ve seen how to link from CompanyList.cfm to CompanyEditForm.cfm and CompanyDeleteForm.cfm, you can do the same with EmployeeList.cfm. The code shows the modifications to make to EmployeeList.cfm in bold.

...
   <table>
   <tr>
   <td><b>Company</b></td>
   <td><b>SSN</b></td>
   <td><b>Name</b></td>
   <td><b>Salary</b></td>
   <td><b>DOB</b></td>
   <td>&nbsp;</td>
   </tr>
   <cfoutput query=”GetEmployees”>
   <tr>
   <td>#CompanyName#</td>
   <td>#SSN#</td>
   <td>#Lastname#, #Firstname#</td>
   <td>#Salary#</td>
   <td>#DateFormat(DateOfBirth, “mm/dd/yyyy”)#</td>
   <td>
   <a href=”EmployeeAddForm.cfm”>Add</a>
   <a
   href=”EmployeeEditForm.cfm?SSN=#URLEncodedFormat(Trim(SSN))#”>Edit</a>
   <a
   href=”EmployeeDeleteForm.cfm?SSN=#URLEncodedFormat(Trim(SSN))#”>Delete<
   /a>
   </td>
   </tr>
   </cfoutput>
   </table>
 ...

You use Trim() on the SSN to make sure that it has no trailing spaces, and you use URL EncodedFormat() to make sure that the SSN is URL-safe. You didn’t need URLEncoded Format() in the company list because CompanyID is an integer, and integers are always URL-safe. You can now go to the employee list and click the Edit or Delete link to go directly to the edit or delete form. Redirecting the user back to the company list The fact that you must go back to the launch pad and then back to the company list every time that you add, edit, or delete a company, all for the sake of a message that really doesn’t tell you much of anything, is annoying. A much better situation would be that the action page redirects you back to the list page, not even passing a message (because you can safely assume that, if no error occurred, the company or employee was successfully inserted, updated, or deleted in the database). Edit the CFLOCATION tags in CompanyAddAction.cfm, CompanyEditAction.cfm, and CompanyDeleteAction.cfm to the following:

<cflocation url=”CompanyList.cfm”>

Then change the CFLOCATION calls in EmployeeAddAction.cfm, EmployeeEditAction.cfm, and EmployeeDeleteAction.cfm to the following:

<cflocation url=”EmployeeList.cfm”>

Test your changes by going to any of these form pages and clicking Submit. They should all take you back to their respective list. After you add a new employee to the list, the capability to send that employee an e-mail welcoming him to the new company would be nice. You use CFMAIL to send out an e-mail message. For the purposes of this example, assume that all employees have an e-mail address in the style first.last@somewhere.com. The code shows in bold the modifications to make to EmployeeAddAction.cfm so it will send a welcome message to the new user.

<cfquery name=”InsertEmployee”
   datasource=”#Request.MainDSN#”>
   ...
   </cfquery>
   <cfmail to=”#Form.Firstname#.#Form.Lastname#@somewhere.com”
   from=”admin@somwhere.com”
   subject=”Welcome to your new company!”>
   Welcome to your new company, #Form.Firstname# #Form.Lastname#!
   </cfmail>
 <cflocation url=”EmployeeList.cfm”>

CFMAIL has three attributes: From, To, and Subject. From tells ColdFusion where this e-mail is coming from; To tells ColdFusion where to send the message; and Subject is what appears in the Subject line. The content between <cfmail> and </cfmail> becomes the body of the message. You may not have the capability to execute this modification unless you have a default mail server set up in ColdFusion Administrator. For information on using ColdFusion Administrator. Even if you do have a correctly configured mail server, you should take care not to send a bunch of useless e-mails all over the Internet! Go to the company list and drill down to the employees of a single company. Click Add Employee and add a new employee to the database. After ColdFusion takes you back to the employee list, you see all the employees in the database instead of just the employees for the original company. A cookie is a variable stored locally on a user’s machine. You can use a cookie to extend the drill-down from the company list to the employee list such that, after the user adds an employee to the database after drilling down and returns to the list, he still sees only the employees of the original company and not all employees in the database. The code shows in bold the modifications to EmployeeList.cfm to set and use a cookie.

<cfif IsDefined(“URL.CompanyID”)>
   <cfcookie name=”CompanyID” value=”#URL.CompanyID#”>
   </cfif>
   <cfquery name=”GetEmployees”
   datasource=”#Request.MainDSN#”>
   SELECT
   c.CompanyName,
   e.SSN,
   e.Firstname,
   e.Lastname,
   e.Salary,
   e.DateOfBirth
   FROM
   Employee e INNER JOIN Company c
   ON e.CompanyID = c.CompanyID
   <cfif IsDefined(“Cookie.CompanyID”)>
   WHERE
   e.CompanyID = #Val(Cookie.CompanyID)#
   </cfif>
   ORDER BY
   c.CompanyName,
   e.Lastname,
   e.Firstname
   </cfquery>
 ...

If URL.CompanyID is defined, CFCOOKIE creates a cookie on your computer that stores URL.CompanyID. CFCOOKIE has a Name attribute, which specifies the name of the cookie variable, and a Value attribute, which specifies the value that is stored in the cookie. Now look at the CFQUERY statement. Notice how URL.CompanyID has changed to Cookie.CompanyID. You set a cookie variable by using the CFCOOKIE tag, but you read a cookie variable by using the Cookie. prefix. The next time that you drill down to a company’s employees and then perform some action, you see the employee list filtered by the same company after the action page redirects. The problem now is that even if you go back to the launch pad and click the employee list, you still get only the employees of the most recently listed company. The first thing that you must do is modify the link in index.cfm to tell the employee list to show all employees.

<html>
   <head>
   <title>ColdFusion MX Bible</title>
   <link rel=”stylesheet” href=”styles.css”>
   </head>
   <body>
   <h1>Companies</h1>
   <a href=”CompanyList.cfm”>List Companies</a><br>
   <a href=”CompanyAddForm.cfm”>Add a Company</a><br>
   <a href=”CompanyGetEditForm.cfm”>Edit a Company</a><br>
   <a href=”CompanyGetDeleteForm.cfm”>Delete a Company</a>
   <h2>Employees</h2>
   <a href=”EmployeeList.cfm?ShowAll=1”>List Employees</a><br>
   <a href=”EmployeeAddForm.cfm”>Add an Employee</a><br>
   <a href=”EmployeeGetEditForm.cfm”>Edit an Employee</a><br>
   <a href=”EmployeeGetDeleteForm.cfm”>Delete an Employee</a>
   </body>
 </html>

Next, you must modify the employee list to take the ShowAll parameter into account.

<cfif IsDefined(“URL.CompanyID”)> <cfcookie name=”CompanyID” value=”#URL.CompanyID#”>

</cfif> <cfif IsDefined(“URL.ShowAll”)> <cfcookie name=”CompanyID” expires=”NOW”> </cfif> <cfquery name=”GetEmployees” datasource=”#Request.MainDSN#”> . . . <cfif IsDefined(“Cookie.CompanyID”) AND Len(Trim(Cookie.CompanyID))> WHERE e.CompanyID = #Val(Cookie.CompanyID)# </cfif> . . . </cfquery> ...

Now, if URL.ShowAll is defined as you execute EmployeeList.cfm, the CompanyID cookie is deleted from the user’s browser and all employees in the database are displayed. Deleting a cookie from the user’s browser sets its corresponding value in ColdFusion to a blank string rather than deleting it, so in essence Cookie.CompanyID still exists, but its value is a blank string. For this reason we must test the length of the trimmed value of Cookie.CompanyID as well as its value to see if it is valid. If we don’t add the Len(Trim(Cookie.CompanyID)) test to account for a blank cookie value, the query includes the WHERE clause and as a result returns no matching employees because Val(Cookie.CompanyID) is zero.

legal notice

Our website is not responsible for the information contained by this article. Web-articles is a free articles resource.
Suggestion: If you need fresh, daily updated content for your website, feel free to use our service. Click here for more information.

Useful tools and features

Making direct links to the forms  
If you like this article (tutorial), please link to it from your web page using the information above.

related articles

1. Building the company add action template
In this article, you create the template that puts your form data into the database. Create a file named CompanyAddAction.cfm inside the Ch02 directory, type the code into the file’s editing window, and save the file. <cfquery name=”InsertCompany” datasource=”#Request.MainDSN#”> INSERT INTO Company( CompanyName, Address, City, State, ZipCode, Comments ) VALUES ( ‘#Trim(Form.CompanyName)#’, ‘#Trim(Form.Address)#’, ...

2. Modifying a Company in the Database
The data that CFQUERY requests from the database comes back in a result set, and the Name attribute tells ColdFusion what that result set is to be named. The SQL statement consists of three clauses: SELECT, FROM, and ORDER BY. SELECT tells the database which columns to retrieve from the database; FROM tells the database which table to retrieve those columns from; and ORDER BY tells the database how to sort the results. The result set returned from CFQUERY contains multiple rows of data, and each row has multiple columns. It would ...

3. Building the company edit action template
The code in CompanyEditAction.cfm is like the code in CompanyAddAction.cfm, but the edit action updates rather than inserts. Create a file named CompanyEditAction.cfm inside the Ch02 directory, type the code into the file’s editing window, and save the file. <cfquery name=”UpdateCompany” datasource=”#Request.MainDSN#”> UPDATE Company SET CompanyName = ‘#Trim(Form.CompanyName)#’, Address = ‘#Trim(Form.Address)#’, City = ‘#Trim(Form.City)...

4. Adding a New Employee to the Database
The CFQUERY in CompanyDeleteAction.cfm uses a DELETE statement with two SQL clauses: DELETE and WHERE. DELETE tells the database the table from which to delete a record, and WHERE tells the database which record to delete. You can watch the company delete process in action. Point your Web browser to http://<yourserver>/CFMXBible/Ch02/CompanyGetDeleteForm.cfm and enter a CompanyID. (To get a valid ID, go to the company list and pick a number from the ID column.) Click Submit to see the chosen company’s informatio...

5. Modifying an Employee in the Database
The DateFormat() function around the DateOfBirth column in the codereturns the employee’s birth date reformatted according to a display mask. DateOfBirth normally comes back from the database in the following format: 2002-01-01 00:00:00.0 That format is not very user-friendly. Calling DateFormat() with a mask of “mm/dd/yyyy” returns the date as follows: 01/01/2002 This version is, of course, more natural and easy to read. The same is true for the employee edit process...

6. Removing an Employee From the Database
The user must have the capability to remove employees from the database. The employee delete process is a simple combination of techniques that you have already learned, such as retrieving a record from the database, displaying that record in a template, and so on. The first page in this process is nearly identical to the employee get edit form. Create a file named EmployeeGetDeleteForm.cfm inside the Ch02 directory, type the code into the file’s editing window, and save the file. <html> <head> <ti...

7. Using a Custom Tag
Custom tags are reusable, developer-authored extensions to the ColdFusion language. The custom tag that you create in the following parts displays today’s date in a familiar format. Create a file named TodaysDate.cfm inside the Ch02 directory, type the code into the file’s editing window, and save the file. <cfoutput>#DateFormat(Now(), “ddd, mmm d, yyyy”)#</cfoutput> Now() returns the current date and time, and DateFormat() reformats the date. To call your custom tag, open index.cfm, a...

8. Creating your physical data model
Physical data models are extracted from their logical counterparts by mapping logical objects to physical objects supported by the target database platform. The following table shows the relationship between logical and physical objects. In many cases, your physical data model almost mirrors your logical data model because entities typically map directly to the tables that store them. Exceptions to this rule are logical data models that contain nonspecific relationships and entity subtypes. Although entity subtypes are an advanced top...

9. Understanding All Relational Result Sets
This article can help you better understand complicated SQL containing multitable joins, group-related clauses, and aggregate functions—by far the most problematic topics for most database developers. You also learn the correct way to handle database exceptions and incorporate them as actual functionality in your ColdFusion application. You learn, too, how to increase performance by caching queries in memory for fast access. You can memorize SQL clauses and Bachus-Naur forms until you’re blue in the face, ...