Help on processing a dynamic set of checkboxes

Ask a Question related to Coldfusion - Advanced Techniques, Design and Development.

  1. #1

    Default Help on processing a dynamic set of checkboxes

    Hello everyone!

    I am having a doozy of a time trying to figure out how to process some
    checkboxes that are dynamically created.
    Here is my situation:

    I have a database table that stores questions for a survey (tblQuestions) and
    another table that stores the answers (tblAnswers) for the questions.
    tblQuestions contains the following fields; (QuestionID, Question, Type,
    Responses, isActive) and tblAnswers contains the following fields (AnswerID,
    RespondentID, QuestionID, Answer).

    tblQuestions and tblAnswers are related to each other through the QuestionID
    field (QuestionID is a primary key in tblQuestions and a foreign key in
    tblAnswers). There is a special field in tblQuestions that i am using to store
    a list of responses that are supposed to display next to the checkboxes in my
    checkbox set. This list is delimited by a semi-colon ( ; ) and I use a
    <cfloop> to run through the list and display it in my form. Here is the code:

    <tr>
    <td class="subtitles" style="color : orange"
    valign="top">Q#CurrentRow#</td>
    <td class="subtitles" valign="top" colspan="3">#question#</td>
    </tr>
    <tr>
    <td></td>
    <td class="subtitles">
    <cfloop index="counter" list="#responses#" delimiters=";">
    <cfinput type="checkbox" name="answer#CurrentRow##counter#"
    value="#counter#">#counter#<br>
    </cfloop>
    </td>
    </tr>

    I am having trouble trying to figure out how my action page is going to
    process this dynamic checkbox set. I want the action page to figure out what
    items where checked and create a semi-colon delimited list of the values of
    checked checkboxes.

    Does anyone have an idea of how to accomplish this? Or any other ideas?
    Thank you in advance and have a wonderful day


    In case youre interested, here is the code to both my form and action page so
    far if it will help. The code is by no means complete and still has to be
    ironed out but the issue im working on right now is detailed above.

    ----------------THE FORM -------------------------
    ---------------------------------------------------------


    <cfquery name="CommonResidentQuestions" datasource="#application.ds#">
    SELECT QuestionID, HasSubQuestion, question, type, responses, subQuestion
    FROM BS_Questions
    WHERE isGeneral = 1 AND
    isRA = 0 AND
    isActive = 1
    </cfquery>

    <cfquery name="MonthlyResidentQuestions" datasource="#application.ds#">
    SELECT QuestionID, HasSubQuestion, question, type, responses, subQuestion
    FROM BS_Questions
    WHERE #URL.Month# = 1 AND
    isActive = 1
    </cfquery>

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <link href="../style_ash.css" rel="stylesheet" type="text/css">
    </head>

    <body>

    <cfform action="bs_RAResidentQuestions_insert.cfm"
    name="RAResidentQuestionsForm" scriptSrc="../cfform.js">
    <table align="center" width="500" border="0">
    <cfoutput>
    <tr class="tableh2">
    <td colspan="4"><font size="+1">Questions for Resident :
    <br>#URL.resFirstName# #URL.resLastName#</font></td>
    </tr>
    <tr>
    <td colspan="4"><b>Monthly Questions</b></td>
    </tr>
    </cfoutput>
    <cfoutput query="CommonResidentQuestions">
    <cfif #type# is 'yesno'>
    <tr>
    <td class="subtitles" style="color:orange" valign="top">Q#CurrentRow#</td>
    <td class="subtitles" valign="top">#question#</td>
    <td class="subtitles" style="color:orange"><cfinput type="radio"
    name="answer#CurrentRow#"
    value="Yes"
    required="yes">Yes</td>
    <td class="subtitles"style="color:orange"><cfinput type="radio"
    name="answer#CurrentRow#"
    value="No"
    required="yes">No</td>
    <input type="hidden" name="questionID#CurrentRow#" value="#questionID#">
    </tr>
    <cfif #HasSubQuestion# is 1>
    <tr>
    <td class="subtitles"></td>
    <td class="subtitles" colspan="3">#SubQuestion#</td>
    </tr>
    <tr>
    <td colspan="4"><textarea rows="5"
    cols="50"
    style="width: 500px"
    name="subanswer#CurrentRow#"
    class="inputBox"></textarea></td>
    </tr>
    <input type="hidden" name="subquestionID#CurrentRow#"
    value="#questionID#">
    </cfif>
    <tr><td height="20px"></td></tr>

    <cfelseif #type# is 'open'>
    <tr>
    <td class="subtitles" style="color:orange"
    valign="top">Q#CurrentRow#</td>
    <td colspan="3" class="subtitles" valign="top">#question#</td>
    </tr>
    <tr>
    <td colspan="4"><textarea rows="5"
    cols="50"
    style="width: 500px"
    name="answer#CurrentRow#"
    class="inputBox"></textarea>
    </td>
    </tr>
    <input type="hidden" name="questionID#CurrentRow#" value="#questionID#">
    <cfif #HasSubQuestion# is 1>
    <tr>
    <td class="subtitles"></td>
    <td class="subtitles" colspan="3">#SubQuestion#</td>
    </tr>
    <tr>
    <td colspan="4"><textarea rows="5"
    cols="50"
    style="width: 500px"
    name="subanswer#CurrentRow#"
    class="inputBox"></textarea></td>
    </tr>
    <input type="hidden" name="subquestionID#CurrentRow#"
    value="#questionID#">
    </cfif>
    <tr><td height="20px"></td></tr>

    <cfelseif #type# is 'smalltext'>
    <tr>
    <td class="subtitles" style="color:orange"
    valign="top">Q#CurrentRow#</td>
    <td class="subtitles" valign="top">#question#</td>
    <td colspan="2" class="inputbox"><cfinput type="text"
    name="answer#CurrentRow#"
    required="yes">
    </td>
    <input type="hidden" name="questionID#CurrentRow#" value="#questionID#">
    </tr>
    <cfif #HasSubQuestion# is 1>
    <tr>
    <td class="subtitles"></td>
    <td class="subtitles" colspan="3">#SubQuestion#</td>
    </tr>
    <tr>
    <td colspan="4"><textarea rows="5"
    cols="50"
    style="width: 500px"
    name="subanswer#CurrentRow#"
    class="inputBox"></textarea></td>
    </tr>
    <input type="hidden" name="subquestionID#CurrentRow#"
    value="#questionID#">
    </cfif>
    <tr><td height="20px"></td></tr>


    <cfelseif #type# is 'check'>
    <tr>
    <td class="subtitles" style="color:orange"
    valign="top">Q#CurrentRow#</td>
    <td class="subtitles" valign="top" colspan="3">#question#</td>
    </tr>
    <tr>
    <td></td>
    <td class="subtitles">
    <cfloop index="counter" list="#responses#" delimiters=";">
    <cfinput type="checkbox" name="answer#CurrentRow##counter#"
    value="#counter#">#counter#<br>
    </cfloop>
    </td>
    <td></td>
    <td></td>
    </tr>
    <cfif #HasSubQuestion# is 1>
    <tr>
    <td class="subtitles"></td>
    <td class="subtitles" colspan="3">#SubQuestion#</td>
    </tr>
    <tr>
    <td colspan="4"><textarea rows="5"
    cols="50"
    style="width: 500px"
    name="answer#CurrentRow#"
    class="inputBox"></textarea>
    <textarea rows="5"
    cols="50"
    style="width: 500px"
    name="subanswer#CurrentRow#"
    class="inputBox"></textarea></td>
    </tr>
    <input type="hidden" name="subquestionID#CurrentRow#"
    value="#questionID#">
    </cfif>
    <tr><td height="20px"></td></tr>
    </cfif>
    </cfoutput>
    <tr>
    <td colspan="4" align="center"><input type="submit" value="Submit Answers"
    class="SubButtons" onMouseOver="this.className='subButtons2'"
    onMouseOut="this.className='subButtons'"></td>
    </tr>
    <!--- Pass these hidden values to the action page --->
    <cfoutput>
    <input type="hidden" name="MonthNoID" value="#URL.MonthNoID#">
    <input type="hidden" name="Month" value="#URL.Month#">
    <input type="hidden" name="residentID" value="#URL.residentID#">
    <input type="hidden" name="NumberOfQuestions_Commom"
    value="#CommonResidentQuestions.RecordCount#">
    <input type="hidden" name="NumberOfQuestions_Monthly"
    value="#MonthlyResidentQuestions.RecordCount#">
    </cfoutput>
    </table>
    </cfform>

    </body>
    </html>


    ----------------- THE ACTION PAGE ---------------------------------
    ------------------------------------------------------------------------------

    <cfquery name="CommonQuestions" datasource="#application.ds#">
    SELECT questionID
    FROM BS_Questions
    WHERE isGeneral=1 AND
    isActive=1
    </cfquery>

    <cfquery name="MonthlyQuestions" datasource="#application.ds#">
    SELECT questionID
    FROM BS_Questions
    WHERE #FORM.Month# = 1 AND
    isActive=1
    </cfquery>

    <cfloop from="1" to="#FORM.NUMBEROFQUESTIONS_COMMOM#" index="row">
    <cfset currentQuestionID=FORM["questionID" & row]>
    <cfset currentAnswer=FORM["answer" & row]>

    <cfquery name="CommonQuestions" datasource="#application.ds#">
    INSERT INTO
    BS_Answers(MonthNoID, residentID, questionID, answer)
    VALUES
    ('#FORM.MonthNoID#',
    '#FORM.residentID#',
    #currentQuestionID#,
    '#currentAnswer#')
    </cfquery>
    </cfloop>

    MajikMerlin Guest

  2. Similar Questions and Discussions

    1. Dynamic Checkboxes
      This is a rather simple situation that is killing my brain, maybe because I'm working on a Sunday night?! Anyhow, I am running a query against my...
    2. Deselecting dynamic checkboxes
      I am quering a database that dynamically creates a table with three columns. The first column is the ID, the second is the title of the document and...
    3. Dynamic Checkboxes in Rich Forms
      Hi, I am just in the process of working with rich forms in CF MX7. I am trying to display checkboxes created from a query. The form is displayed,...
    4. Dynamic checkboxes and INSERT into DB
      "Frank Collins" <fcollins@mhca.com> wrote in message news:094101c352a8$7ca66990$a401280a@phx.gbl... Hmm..so you get your companyID on the same...
    5. Dynamic checkboxes & INSERT into DB
      Can anyone point me to some good examples on the web of using values from dynamically created checkboxes on forms in ASP, particularly relating to...
  3. #2

    Default Re: Help on processing a dynamic set of checkboxes

    Originally posted by: MajikMerlin
    tblQuestions and tblAnswers are related to each other through the QuestionID
    field (QuestionID is a primary key in tblQuestions and a foreign key in
    tblAnswers). There is a special field in tblQuestions that i am using to store
    a list of responses that are supposed to display next to the checkboxes in my
    checkbox set. This list is delimited by a semi-colon ( ; ) and I use a
    <cfloop> to run through the list and display it in my form.


    I haven't read all of the code, but before you go any further, you may want to
    re-examine your table structure. As you can see from your struggles so far,
    storing information as delimited lists is usually not the right way to go ;-)
    Aside from being difficult to work with, using that structure prevents you from
    truly harnessing the power of your database.

    Take a look at the OP's table structure in this post. The question itself is
    not related, but the overall purpose (survey questions) is similar to what you
    are trying to do:


    [url]http://groups-beta.google.com/group/macromedia.coldfusion.getting_started/tree/b[/url]
    rowse_frm/thread/8d1213dbefbf113a/ef8e72a9545ec69e?#doc_ef8e72a9545ec69e

    mxstu Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139