|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Creating an HTML Query Form
|
|
|
|
The most common way to create dynamic parameters is with a form serving as the user interface. You can build HTML forms to include user entry text boxes, select boxes, radio buttons, and checkboxes for users to enter information. Forms are easy to create and offer a number of flexible data input options. The form variables that are automatically created when a form is submitted can be used to create queries to your data source.
In addition to HTML forms, you can also create forms using the ColdFusion CFFORM tag. See Chapter 12, Building Dynamic Java Forms if you want to build custom forms that contain graphical elements like tree view controls, slider controls, and grid display controls.
|
|
|
|
Example: Query form
|
|
|
The following example demonstrates a simple HTML form:
<FORM ACTION="employeesearch.cfm" METHOD="Post">
<PRE>
Last Name: <INPUT TYPE="text"
NAME="LastName">
Department:
<SELECT NAME="Department">
<OPTION>Accounting
<OPTION>Administration
<OPTION>Engineering
<OPTION>Sales
</SELECT>
<INPUT TYPE="Submit" VALUE="Search">
</PRE>
</FORM>
The form above has two inputs: LastName and Department. The user can fill in the text area with a last name and select a department from the select list. Each OPTION tag lists a possible selection the user can make. When the submit button is clicked, the ColdFusion page specified in the ACTION attriubte is opened, and the form variables are passed to the page as dynamic parameters.
Suppose the user enters the name "Peterson" and chooses "Sales." When she clicks the submit button, the form variables shown below will be sent to the application page employeesearch.cfm :
LastName=Peterson
Department=Sales
|
|
|
|
Set the form's ACTION and METHOD attributes |
|
|
|
You must set the form's ACTION attribute. The ACTION attribute in your HTML form tells the browser which application page to call when the user clicks a submit button. You also must set the form's METHOD attribute to Post. In the following example, the employeesearch.cfm application page is executed when the user submits the form for processing.
<FORM ACTION="employeesearch.cfm" METHOD="Post">
|
|
|
|
Implement data query fields |
|
|
|
Creating search fields for an HTML form is very simple. You need only implement the HTML form fields for each database column you want to search. To make your application pages more legible, it is helpful to make the form field names identical to your database column names.
For example, if you have a table called Employees with three columns called FirstName, LastName, and Department, your form fields might look like this:
Name: <INPUT TYPE="text" NAME="FirstName">
LastName: <INPUT TYPE="text" NAME="LastName">
Department: <INPUT TYPE="text" NAME="Department">
In your forms, you can use the full range of HTML input widgets, including list boxes, radio buttons, check boxes, and multi-line text boxes, in your forms.
|
|
|
|
REQUESTTIMEOUT URL parameter
|
|
|
When passing a request for a ColdFusion page, you can use the REQUESTTIMEOUT parameter in the URL to specify the number of seconds before the data source connection times out. The REQUESTTIMEOUT parameter overrides the default timeout specified in the ColdFusion Administrator. This option can prevent a data source connection from timing out for operations that need more connection time to complete.
To use this parameter, you pass a URL for an application page with the REQUESTTIMEOUT parameter specifying the number of seconds before timing out, in the following form:
http://myserver.com/cfpages?REQUESTTIMEOUT=100
This parameter is typically used for administrative tasks such as data source updates and is not necessary for normal processing.
|
|
|
|
Example: Dynamic SQL
|
|
|
The example application page below, searchform.cfm , can be created as a query form.
<HTML>
<HEAD>
<TITLE>Employee Search</TITLE>
</HEAD>
<BODY>
<FORM ACTION="employeesearch.cfm" METHOD="Post">
<PRE>
Last Name: <INPUT TYPE="text" NAME="LastName">
Department: <SELECT NAME="Department">
<OPTION>Accounting
<OPTION>Administration
<OPTION>Engineering
<OPTION>Sales
</SELECT>
<INPUT TYPE="Submit" VALUE="Search">
</PRE>
</FORM>
</BODY>
</HTML>
The example application page below (EmployeeSearch.cfm ) can be created to select and display the search.
<!--- CFML application page to implement
employee search --->
<!--- Query the database --->
<CFQUERY NAME="EmployeeList"
DATASOURCE="CompanyDB">
SELECT *
FROM Employees
WHERE LastName = '#LastName#'
AND Department = '#Department#'
</CFQUERY>
<!--- Page header --->
<HTML>
<HEAD>
<TITLE>Employee Search Results</TITLE>
</HEAD>
<BODY>
<H2>Organization Search Results</H2>
<!--- Summarize search criteria for user --->
<CFOUTPUT>
<P>The search for #Form.LastName# in
the #Form.Department#
returned these results:</P>
</CFOUTPUT>
<--- Display results --->
<CFOUTPUT QUERY="EmployeeList">
<HR>
#FirstName# #LastName# (Phone: #PhoneNumber#)<BR>
</CFOUTPUT>
<!--- Page footer --->
<P>
Thank you for searching the
employee database!</P>
<HR>
</BODY>
</HTML>
|
|
|
|
Pattern matching searches |
|
|
|
When you provide your users the ability to enter text values as part of a search front end, you normally do not want to search for exactly the value they have entered. Rather, you want to search for a value similar to what they entered. In this case, a pattern matching search is appropriate.
To implement a pattern matching search, use the LIKE operator in combination with one or more wildcard characters (represented by "%" in ODBC queries).
For example, to allow users to search for people by filling out a form field called "LastName," use the SQL statement:
SELECT *
FROM Employees
WHERE LastName LIKE '#LastName#%'
This query returns the record of every person whose last name begins with the value entered. For example, a search for "Jon" would return records for people with names beginning with "Jon," including "Jones" and "Jonson."
|
|
|
  
|
|
|
AllaireDoc@allaire.com
Copyright © 1998, Allaire Corporation. All rights reserved.
|