×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

CFLOOP and/or Checkboxes

Dealing with a group of checkboxes by webmigit
Posted: 6 Nov 02 (Edited 6 Feb 04)

This tutorial works best with applications such as email systems or user admin.

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:

<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...

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


This is probably what your current cfoutput looks like:

<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:

<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.

<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#.

<!---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:

<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> 

Back to Adobe FAQ Index
Back to Adobe Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close