Skip to main content
Participant
July 16, 2008
Question

Trouble processing dynamically generated checkboxes

  • July 16, 2008
  • 2 replies
  • 264 views
hello. this is my first time working with coldfusion for a project. pardon me if i don't know anything remotely advanced.

the thing is this:
i'm trying to create a page in which i can use it to create an account with lecturer permissions. the user will be keying in the username, password. then there is this list of checkboxes for all the modules so that the user can check all the modules the owner of the lecturer account is teaching. this is so that the lecturer can only upload module material for modules that he or she is teaching.

upon submitting the page, the data source will then be updated by inserting multiple records according to the checkboxes checked. for example, i have 3 checkboxes, 1, 2, 3. i check box 1 and 3. when sent, the data source will store 2 data entries, userID=1/moduleID=1 and userID=1/moduleID=3.

but i can't get the processing of the checkboxes to work. here's the codes for both pages, addUSElect.cfm and addUSElect2.cfm

addUSElect.cfm
============
<!--get checkboxes--!>
<cfquery datasource="fypdatabase" name="moduleCHECKlist">
SELECT *
FROM moduleTABLE, courseTABLE
WHERE moduleTABLE.courseID = courseTABLE.courseID
ORDER BY courseTABLE.courseID
</cfquery>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns=" http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form action="addUSElect2.cfm" method="post" name="FORM" id="FORM">
<table width="431" border="0" cellpadding="4" cellspacing="3">
<tr>
<td colspan="2" align="center"><strong>Create New Lecturer</strong></td>
</tr>
<tr>
<td width="140" align="right" valign="top">Username: </td>
<td width="266" valign="top"><label>
<input type="text" name="useTEXT" id="useTEXT" />
</label></td>
</tr>
<tr>
<td align="right" valign="top">Password: </td>
<td valign="top">
<input type="password" name="passTEXT" id="passTEXT" />
</td>
</tr>

<tr>
<td align="right" valign="top">Modules: <br />
(Select all that applies) </td>
<td align="left" valign="top"><table width="100%" border="0" cellpadding="0" cellspacing="0">
<cfloop query="moduleCHECKlist">
<tr>
<td width="21" align="left" valign="top">
<cfoutput>
<input type="checkbox" name="check" value="#moduleCHECKlist.moduleID#" />
</cfoutput>
</td>
<td width="165" align="left" valign="top">
<cfoutput>#moduleCHECKlist.moduleNAME# (#moduleCHECKlist.courseCODE#)</cfoutput>
</td>
</tr>
</cfloop>
</table></td>
</tr>

<tr>
<td colspan="2" align="center"><label>
<input type="submit" name="submit" id="submit" value="Create" />
</label></td>
</tr>
</table>

</form>
</body>

addUSElect2.cfm (i'm putting cfoutput yes and no to speed things up when testing whether the checking works)
=============
<!--to ensure that the number and order of loops is same as the list o checkboxes--!>
<cfquery datasource="fypdatabase" name="moduleCHECKlist">
SELECT *
FROM moduleTABLE, courseTABLE
WHERE moduleTABLE.courseID = courseTABLE.courseID
ORDER BY courseTABLE.courseID
</cfquery>

<cfloop query="moduleCHECKlist">

<cfif trim(check) IS "#moduleCHECKlist.moduleID#">
<cfoutput>Yes</cfoutput>

<cfelse>
<cfoutput>No</cfoutput>
</cfif>

</cfloop>
</html>

please help me in anyway you can to solve this conundrum.
    This topic has been closed for replies.

    2 replies

    Inspiring
    July 16, 2008
    Azadi's answer is correct but incomplete. The big thing is that if the user submits the form without checking any check boxes, the form field won't exist. Your code has to be prepared for that.

    He mentioned inserting multiple rows at once. Depending on the db, that may be faster or slower than individual queries. The way to do it also depends on the db. When I do it, the query resembles this:

    insert into sometable
    (fields)
    <cfloop>
    select distinct values
    from some_small_table
    <cfif loop not done>
    union
    closing tags
    Inspiring
    July 16, 2008
    you are off to a vary good start.
    you have your checkboxes all with the same name (check) and unique
    values (moduleID) - makes it easy to work with.

    a few thing to consider and help you along:
    - try to scope your variables. use form.check instead of just check on
    your action page - it makes cf find the values faster and will help you
    avoid variables confusion if you have same-named variables in diferent
    scopes.
    - don't give your form NAME and ID of "FORM" - that's bound to confuse
    someone (like you) or something (like CF, which has a built-in FORM
    scope)... use some other name, like FORM1
    - since all your checkboxes have the same name, the form will submit
    their values as a comma-delimited list. you can just loop over this list
    to insert the data into your db:

    <cfloop list="#form.check#" index="module">
    <cfquery ..>
    INSERT INTO ...
    (userID, moduleID)
    VALUES
    (#your var holding userID#, #module#)
    </cfquery>
    </cfloop>

    depending on your db the above code can differ and be significantly
    better optimized, i.e inserting multiple rows at once (inside one
    cfquery tag)

    hth


    Azadi Saryev
    Sabai-dee.com
    http://www.sabai-dee.com/