Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dealing with a group of checkboxes

CFLOOP and/or Checkboxes

Dealing with a group of checkboxes

by  webmigit  Posted    (Edited  )
[color red]This tutorial works best with applications such as email systems or user admin.[/color]

Sometimes when you have an operation for multiple items, such as deleting certain emails from a whole list, the convenient way is to have checkboxes next to each item..

You'll notice almost all web based email systems have this.

The logic is pretty simple but, all the same, I'm going to help you with it.

Let's start this correctly.. The first two lines should be:

Code:
<CFPARAM name="Action" default="">
<CFPARAM name="form.delID" default="">

This essentially tells Cold Fusion that its ok for both or either variable to be empty or null.

Now let's say you have a list of messages for each user in a database...

Your query to get the messages probably looks something like this...

Code:
<CFQUERY name="GetMessages" datasource="#request.dsn#">
 SELECT * FROM Messages
 WHERE Recip=#userID#
</CFQUERY>

This is probably what your current cfoutput looks like:

Code:
<CFOUTPUT query="GetMessages">
 #Subject# --- #DateSent#<BR>
</CFOUTPUT>

In order to delete now, you probably have a delete link for each message, which is fine for one or two, but fifty? The server load is heavier and more time is taken for the user...

Let's alter the above:

Code:
<CFOUTPUT query="GetMessages">
 <INPUT type="checkbox" name="delID" value="#MessageID#"> #Subject# --- #DateSent#<BR>
</CFOUTPUT>

Ok, now we've got the checkboxes, below the list should of course be a submit button, and the form should be wrapped in a form tag.

Code:
<FORM action="MsgList.cfm" method="post">
 <CFOUTPUT query="GetMessages">
  <INPUT type="checkbox" name="delID" value="#MessageID#"> #Subject# --- #DateSent#<BR>
 </CFOUTPUT>
 <INPUT type="hidden" name="Action" value="Delete Checked Messages">
 <INPUT type="submit" value="Delete checked messages"> (See my FAQ: faq232-2086 for why I do this)
</FORM>

Ok, now following that logic, the front-end is all set, now we'll want to actually run the delete operation.. Following my example above, we place the code in MsgList.cfm above the output queries and code but below the cfparams (Before the output select queries so that our deleted messages)...

The form sent us a list of checked message IDs.. to get this we'll call #form.delID#.

Code:
<!---If Action is our button's value, thus the action button has been clicked.. And if delID is not null--->
<CFIF Action is Delete Checked Messages" AND len(delID) gt 0>
 <!--- The Query --->
 <CFQUERY datasource="#request.dsn#">
  DELETE * FROM Messages
  WHERE MessageID in (#delID#)
    AND Recip=#UserID#
   <!--- The above ensures that Users don't try to delete each others messages --->
 </CFQUERY>
</CFIF>

And now.. the full, compiled, corrected order contents of MsgList.cfm:

Code:
<CFPARAM name="Action" default="">
<CFPARAM name="form.delID" default="">

<!---If Action is our button's value, thus the action button has been clicked.. And if delID is not null--->
<CFIF Action is Delete Checked Messages" AND len(delID) gt 0>
<!---Set the CFLOOP--->
<CFLOOP list="#form.delID#" index="delI">
<!--- The Query --->
<CFQUERY datasource="#request.dsn#">
DELETE * FROM Messages
WHERE MessageID=#delI#
AND Recip=#UserID#
<!--- The above ensures that Users don't try to delete each others messages --->
</CFQUERY>
</CFLOOP>
</CFIF>

<CFQUERY name="GetMessages" datasource="#request.dsn#">
SELECT * FROM Messages
WHERE Recip=#userID#
</CFQUERY>

<FORM action="MsgList.cfm" method="post">
<CFOUTPUT query="GetMessages">
<INPUT type="checkbox" name="delID" value="#MessageID#"> #Subject# --- #DateSent#<BR>
</CFOUTPUT>
<INPUT type="hidden" name="Action" value="Delete Checked Messages">
<INPUT type="submit" value="Delete checked messages">
</FORM>[/code]
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top