| [<--Prev] [Next-->] | ||
| [Printable Version] [Search: ] [Daryl's TCP/IP Primer] [Daryl's ColdFusion Blog] |
![]() |
(Coming Soon?)
Checkboxes are simple user interface elements; the user clicks on a box to "check" it, and clicks again to uncheck. Perfect for those yes/no fields, and also useful for more complex expressions of data, such as the ability to select several of the same type of data, as in "Check the products you are interested in."
The checkbox input element has four attributes: Type must be "Checkbox", a Name must be assigned to the checkbox, and a Value must be assigned to the checkbox. Optionally, the keyword "Checked" can be added to indicate that the checkbox should be pre-checked when the form is initially displayed. Example:
<INPUT Type="Checkbox" Name="ffMyCheckbox" Value=1 Checked>
There are three possible results posted by checkboxes:
This example asks the user for an e-mail address, and prompts the user to check a box if they don't want new product notifications.
The input form:
<FORM Action="FormHandler.cfm" Method="Post"> Please enter your e-mail address: <INPUT Type="Text" Name="ffEmail" Size="30" Maxlength="100"><br> <INPUT Type="Checkbox" Name="ffNoMailings" Value=1>I don't want to receive updates via e-mail.<br> <INPUT Type="Submit" Value=" OK "> </FORM>
The Form handler:
<!----------------------------------->
<!--- Variable Initialization --->
<!----------------------------------->
<CFPARAM Name="ffEmail" Default="">
<CFIF ffEmail DOES NOT CONTAIN "@">
<CF_UserError Message="The e-mail address you entered (#ffEmail#) " &
"does not appear to be a valid e-mail address.">
</CFIF>
<CFPARAM Name="ffNoMailings" Default=0>
<CFIF val(ffNoMailings)>
<CFSET ffNoMailings = 1>
<CFELSE>
<CFSET ffNoMailings = 0>
</CFIF>
<!------------------------->
<!--- Query Section --->
<!------------------------->
<CFQUERY Name="qCheckForEmail" DataSource="#DSN#">
SELECT *
FROM MailingList
WHERE Email = '#ffEmail#'
</CFQUERY>
<CFIF qCheckForEmail.RecordCount>
<!--- user already exists, update --->
<CFQUERY Name="qUpdateEmail" DataSource="#DSN#">
UPDATE MailingList
SET NoMailings = #ffNoMailings#
WHERE Email = '#ffEmail#'
</CFQUERY>
<CFELSE>
<!--- user not in database, insert --->
<CFQUERY Name="qInsertEmail" DataSource="#DSN#">
INSERT INTO MailingList (
Email
,NoMailings
) VALUES (
'#ffEmail#'
,#ffNoMailings#
)
</CFQUERY>
</CFIF>
This example asks the user for an e-mail address, and prompts the user to check boxes for each product they want information on. The user's selections will then be mailed to sales@mycompany.com.
The input form:
<!------------------------->
<!--- Query Section --->
<!------------------------->
<CFQUERY Name="qProducts" DataSource="#DSN#">
SELECT ProductName
FROM Products
</CFQUERY>
<!--------------------------->
<!--- Display Section --->
<!--------------------------->
<FORM Action="FormHandler.cfm" Method="Post">
Please enter your Name:
<INPUT Type="Text" Name="ffName" Size="30" Maxlength="100"><br>
Please enter your e-mail address:
<INPUT Type="Text" Name="ffEmail" Size="30" Maxlength="100"><br>
Please check all products you want more information on:<br>
<CFOUTPUT Query="qProducts">
<INPUT Type="Checkbox" Name="ffProductName"
Value="#qProducts.ProductName#">#qProducts.ProductName#<br>
</CFOUTPUT>
<INPUT Type="Submit" Value=" OK ">
</FORM>
The Form handler:
<!-----------------------------------> <!--- Variable Initialization ---> <!-----------------------------------> <CFPARAM Name="ffName" Default=""> <CFIF trim(ffName) IS ""> <CF_UserError Message="Please enter your name. Lie if you must."> </CFIF> <CFPARAM Name="ffEmail" Default=""> <CFIF ffEmail DOES NOT CONTAIN "@"> <CF_UserError Message="The e-mail address you entered (#ffEmail#) does not appear to be a valid e-mail address."> </CFIF> <CFPARAM Name="ffProductName" Default=""> <CFIF trim(ffProductName) IS ""> <CF_UserError Message="Please select at least one product to receive information on."> </CFIF> <!-------------------------> <!--- Query Section ---> <!-------------------------> <CFMAIL From="#ffEmail#" To="sales@mycompany.com" Subject="[Web Page] Contact #ffName#"> "#ffName#" visited our website on #DateFormat(now(),"m/d/yyyy")# #TimeFormat(now(),"h:mm:tt")#. The e-mail address they entered was "#ffEmail#". They were looking for information on the following products: #ffProductName# Their IP address was #CGI.REMOTE_ADDR#. </CFMAIL>
Let's say the sales department is really happy with your work from examples 1 and 2 (the fact that they were just examples can remain our little secret.) Now they want the mailing list to be somehow tied to the products table, so they can send mailings about products to the people who were interested in the products. So, in addition to the MailingList table from example 1, and the Products table in example 2, we'll create a cross reference table called "MailingListProducts" which will have the fields "Email" and "ProductName".
The input form will remain the same, but the form handler will change a bit:
<!----------------------------------->
<!--- Variable Initialization --->
<!----------------------------------->
<CFPARAM Name="ffName" Default="">
<CFIF trim(ffName) IS "">
<CF_UserError Message="Please enter your name. Lie if you must.">
</CFIF>
<CFPARAM Name="ffEmail" Default="">
<CFIF ffEmail DOES NOT CONTAIN "@">
<CF_UserError Message="The e-mail address you entered (#ffEmail#) " &
"does not appear to be a valid e-mail address.">
</CFIF>
<CFPARAM Name="ffProductName" Default="">
<CFIF trim(ffProductName) IS "">
<CF_UserError Message="Please select at least one product to receive information on.">
</CFIF>
<!------------------------->
<!--- Query Section --->
<!------------------------->
<CFMAIL From="#ffEmail#" To="sales@mycompany.com"
Subject="[Web Page] Contact #ffName#">
"#ffName#" visited our website on #DateFormat(now(),"m/d/yyyy")# #TimeFormat(now(),"h:mm:tt")#.
The e-mail address they entered was "#ffEmail#".
They were looking for information on the following products:
#ffProductName#
Their IP address was #CGI.REMOTE_ADDR#.
</CFMAIL>
<CFQUERY Name="qCheckForEmail" DataSource="#DSN#">
SELECT *
FROM MailingList
WHERE Email = '#ffEmail#'
</CFQUERY>
<CFIF qCheckForEmail.RecordCount>
<!--- user already exists, update --->
<CFQUERY Name="qUpdateEmail" DataSource="#DSN#">
UPDATE MailingList
SET Name = #ffName#
WHERE Email = '#ffEmail#'
</CFQUERY>
<CFELSE>
<!--- user not in database, insert --->
<CFQUERY Name="qInsertEmail" DataSource="#DSN#">
INSERT INTO MailingList (
Email
,Name
) VALUES (
'#ffEmail#'
,'#ffName#'
)
</CFQUERY>
</CFIF>
<!--- now update the user-product xref --->
<CFTRANSACTION>
<CFQUERY Name="DelOldXref" DataSource="#DSN#">
DELETE FROM MailingListProducts
WHERE Email = '#ffEmail#'
</CFQUERY>
<!--- we could, at this point, loop through each list item in ffProductName and perform an insert; --->
<!--- however, we'll use insert/select instead. This has two benefits: --->
<!--- (1) we only run one query --->
<!--- (2) the subquery validates each productname to make sure it's in the --->
<!--- products table, which ensures full data integrity --->
<!--- Change ffProductNames into a quoted, comma-delimited list --->
<CFSET ProductNamesQVL = "'" & replace(replace(ffProductNames,"'","''","ALL"),",","','","ALL") & "'">
<CFQUERY Name="InsNewXref" DataSource="#DSN#">
INSERT INTO MailingListProducts (Email, ProductName)
SELECT '#ffEmail#', ProductName
FROM Products
WHERE ProductName IN (#PreserveSingleQuotes(ProductNamesQVL)#)
</CFQUERY>
</CFTRANSACTION>
Coming Soon?
Coming Soon?