Building the Search Results page Based on the queryable columns identified earlier, the SQL query to display the search results would look like this: SELECT tripName, tripLocation, departureDate, returnDate, price, tripID FROM trips The purpose of the Trip Search form is to supply the data needed to build the WHERE clause to finish this SQL SELECT statement and constrain the query according to the user’s input.
Building a SQL WHERE clause in code is largely an exercise in string concatenation. The & operator combines two strings in ColdFusion. For example, the following code snippet: My name is #FullName#. results in the following text: My name is Dylan Smith.
Reviewing the code The following table describes the code used to build the tripLocation WHERE subclause: Code Explanation The cfset tag initializes the WhereClause variable to hold the WHERE clause to be constructed. The initial value is set to "0=0", so that the WHERE clause has at least one subclause in case the user enters no search criteria.
The Trip Results page displays several entries as follows: c Notice in the Trip Results page that only one trip has a trip location of China. d Click the Back button in your browser to return to the Trip Search page. 3 In the Trip Location drop-down list box of the Trip Search page, select the Is option, enter the value China, then click Search. The Trip Results page displays only one entry for the trip to China in the HTML table.
2 To build the departureDate WHERE subclause, enter the code in the following table immediately following the comment line. For Enter this code Windows users, using the MS Access database file " & Form.
4 Verify that the price and departureDate are now considered in the query, as in step 4 in the previous exercise: a Open the tripsearch.cfm page in the my_app directory in your browser. b In the Departure Date drop-down list box, select Before, enter 1/1/1900 as the date, and select Smaller Than 0 for the price. c Click the Search button. Now, because the departure date is considered in the query, there are no rows returned.
Summary This lesson described how to access a relational database using ColdFusion. You used the SQL SELECT statement and the cfquery and cfoutput tags to display trip lists. You built a search tool that dynamically builds a WHERE clause of the SQL SELECT statement using cfif and cfset tags. To ensure that the SQL statement remains intact, you used the PreserveSingleQuotes CFML function. In the next lesson In the next lesson, you will build the main navigation page for the Trip Maintenance application.
Enhancing the Trip Maintenance application In this lesson you will enhance the Trip Maintenance application that you created in Lesson 2. You will modify the application to include a main application page that lets Compass Travel employees do these tasks: • View additional details about a trip. • Scan records in the Trips database table. • Modify or search for records in the Trips database table.
• Enhanced Trip Search Results page. The original purpose of the Trip Search Results page in Lesson 2 was to display the results of a trip search. In this lesson, you will enhance this page to provide a useful drill-down mechanism for accessing additional information about trips that meet the search criteria. • Main Application page In the beginning of this lesson, you will develop the Trip Detail page (see the following figure).
The primary users of these components will be the Compass Travel coordinators and agents, not the general public. Showing additional trip details By design, the Trip Search Results page displays a subset of the information about a trip. To get additional information about any of the trips displayed, the user should be able to click on any row to display the detailed trip data.
Exercise: building a Trip Detail page Follow these steps to build a Trip Detail page. To build a Trip Detail page: 1 Open your editor and create a new ColdFusion page. Remove any lines if your editor adds.
8 The Rio Cahabon Rafting trip has an tripID of 24. To view the trip detail for the trip in your browser enter one of the following URLs:: Web server configuration URL For stand-alone ColdFusion web server configuration http://localhost:8500/cfdocs/getting_started/my_app/ tripdetail.cfm For local third-party web server configuration http://localhost/cfdocs/getting_started/my_app/ tripdetail.
Reviewing the code The following table describes the ColdFusion code used to build the Trip Detail page: Code Explanation The cfquery tag includes a maxRows attribute. This attribute limits the number of result rows brought back from the database. In the Trip Detail page, we want to only show a single row at a time, therefore, maxRows is set to 1. WHERE tripID = #URL.ID# The URL.
Protecting your application To ensure that your application is protected from such an attack, you can exploit the fact that the ID must be a numeric value. The CFML Val function returns the numeric value at the beginning of a string expression. You can use the Val function as follows: WHERE tripID = #Val(URL.ID)# Now if non-numeric data is passed within the URL ID field, the Val statement returns 0, and the trip with ID 0 displays (if one exists).
3 In the Trip Location drop-down list box, select Begins With and type the value C in the trip location text box then click Search. The Trip Search Results page displays a hyperlink for each trip name listed, as the following figure shows: 4 To view the Trip Detail page for a trip, click on the trip name. You might notice that the dates and prices in both the Trip Detail and Trip Search Results pages are unformatted. You will improve the appearance of the application in the next exercise.
Existing code Change to #price# #dollarformat(price)# #baseCost# #dollarformat(baseCost)# 2 To format the currency and date fields on the Trips Search Results page, open the tripsearchresult.cfm in your editor and make the same changes for departureDate, returnDate, and price as in step 1.
5 In the Trip Search Result page, click the link for Riding the Rockies. The properly formatted Trip Detail page appears: Creating the main application page from the Trip Detail page To this point in the tutorial, you created a very useful drill-down query facility. Compass Travel trip coordinators can produce lists required by management and easily locate and display information about any trip.
Adding navigation buttons to browse database The drill-down search function developed in the last exercise is very useful when the user knows some search criteria to enter. Unfortunately, however, flipping back and forth between the results page and the detail page to navigate through a record set can be tedious. Moreover, on occasion the trip coordinator might want to browse the Trips database just to check for anomalies or to become familiar with its contents.
3 Save the file and view the updated tripdetail.cfm page in a browser. The Trip Search Results page appears: 4 Test the buttons by clicking any navigation button. An error occurs because the navigation action page (navigationaction.cfm) does not exist. The navigation action page processes the navigation button requests. You will build the navigation action page in the next lesson.
Adding database maintenance buttons The search and sequential navigation capabilities are features for locating Compass Travel trips. After the trip coordinator locates a trip, they must be able to modify or delete the trip. Additionally, when viewing the detail for a trip, they must be allowed to add a new trip or use the search facility.
3 Save the file and view the updated tripdetail.cfm page in a browser (http:// localhost/CFDOCS/getting_started/my_app/tripdetail.cfm). The page appears as follows: 4 Click Search or Delete to test the database maintenance buttons. An error occurs because the Maintenance Action page does not exist. The Maintenance Action page is required to process the maintenance button requests. You will develop this page in the next lesson.
Summary In this lesson, you transformed the search facility you built in Lesson 2 into a drill-down facility for trip queries. You built a Trip Detail page to show more information about a particular trip. You also formatted the Trip Search Results and Trip Detail pages using the CFML DollarFormat and DateFormat functions. You linked the Trip Search Results page with the Trip Detail page. You used the URLEncodedFormat CFML function to ensure that data was correctly from one page to the other.
LESSON 4 Validating Data to Enforce Business Rules In this lesson, you will enhance the Compass Travel Trip Maintenance application. The exercises in this lesson will guide you through the steps of enhancing the application to provide a page for the trip coordinator to add new trip offerings and update existing trips. Further, you will add logic to validate that data entered against Compass Travel business rules.
Enhancing the Trip Maintenance application In this lesson and the next, you will create the code to implement the remaining maintenance buttons on the main Trip Maintenance application page. The remaining buttons are Add and Edit. You will develop the data entry form to capture new trip information and validate the data entered against Compass Travel business rules. You will then modify the data entry form to edit existing trips.
Using an HTML form to collect data Based on the data requirements determined in Lesson 1, the following figure shows the Trip Edit data collection page: Exercise: view the source and test the Trip Edit page To view the source and test the Trip Edit data collection form: 1 Open an editor, then locate and open the file tripedit1.cfm in the solutions directory \cfdocs\getting_started\solutions under your web root directory. 2 Review the HTML source code used to create the Trip Edit page.
Tag Description Table You can format a data entry form and display its controls neatly, by using the table tag, table, table row tag, tr, and the table data tag, td. Form Controls The form requires controls to collect and submit user input. There are a variety of types of form controls you can use. For this lesson, you will use the following controls: • . Accepts text answers such as Trip Name and Trip Price. • .
Developing code to validate data and enforce business rules As described in Lesson 1, it is important to define the right data type for each column on the tables in the database. A fundamental concern, therefore, is ensuring that the captured data is suitable for the column definitions in the Trips table. This type of validation on a single field is often referred to as a single-field edit. Compass Travel has other operating policies that involve evaluating the values from more than one field.
Validating data using a server-side action page The first approach you will take to enforce Compass Travel business rules is to develop an action page to validate the data collected on the data entry form. The action page receives a form variable for every field on the form that contains a value. You use the cfif tag to test the values of these fields to ensure that they adhere to Compass Travel business policy.
The cfset tag lets you manipulate the value of a variable. For example, the following pseudocode initializes a variable to a specific value and checks the value using the cfif statement: if rule 1 fails then ... update the database In the previous example, cfset initializes the local variable isOk to Yes. If any rule fails, the variable isOK is set to No.
Price must be marked up at least 20% above cost. Note: The code for business rule 7 uses ColdFusion cfif and cfelse conditional processing tags. The code inside the cfif tags only executes when the condition evaluates to True. To perform other actions when the condition evaluates to False, the cfelse tag is used.
the network and the server. If the data is validated on the client, then only valid data is posted to the server and traffic is reduced. Validating data on the client using ColdFusion form tags An alternative approach to server-side editing is to use client-side scripting. Client-side scripting lets you validate the form data entered on the client prior to posting it to the server. CFML provides alternative versions of standard HTML form tags that provide advantages of client-side data validation.
Client-side validation approach using ColdFusion form tag The following code is on the client: Code Explanation Use the cfinput tag to create the duration input entry field within a cfform.
Tip: For additional help, review the completed code in the tripedit2.cfm within the solutions directory. For more details about using ColdFusion form tags and their attributes, see Developing ColdFusion MX Applications with CFML. 5 In your editor, open the tripeditaction.cfm in the my_app directory and delete the code for the following single-field validation rule: • Trip name is required. • Trip leader is required. • Photo file name is required. • Number of people is required and must be numeric.