Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 128
  • Last Modified:

Dynamic Form Processing

0
jmcarroll
Asked:
jmcarroll
1 Solution
 
anandkpCommented:
huh ???
0
 
jmcarrollAuthor Commented:
I'm not sure how that happened... I posted my question and now I see it reads 'no text'... and I can't edit it.

Here's my question:

I have a form that has a number of checkboxes that are generated dynamically.  I have two problems: how can I 'recall' the values of these checkboxes so that I can check/uncheck them later (the idea of this form is students being assigned to organizations for study, so they might belong to one or more and they may change), but also how do I write the update query in a 'dynamic looping' way given the variable number of checkboxes?

This is what I have so far:

      <cfif IsDefined('form.action')>
            <cfif form.action eq 'add'>
                  <cfif IsDefined("form.org") AND ListLen(form.org) GT 0>
                        <cfloop list="#form.org#" index="i">
                              <cfquery name="addorg" datasource="#DSN#">
                              INSERT INTO org_admin
                              (
                                    users_id,
                                    org_id
                              )
                              VALUES
                              (
                                    #form.users_id#,
                                    '#i#'
                              )
                              </cfquery>
                        </cfloop>
                  </cfif>
            </cfif>
            <cfif form.action eq 'update'>
            </cfif>
      </cfif>

      <!--- GENERIC QUERIES AND VARIABLE SETTING --->

      <cfquery name="list" datasource="#DSN#">
      SELECT       *
      FROM       organizations
      ORDER BY org_abbr
      </cfquery>

      <cfquery name="list_org" datasource="#DSN#">
      SELECT       *
      FROM       org_admin
      WHERE       users_id = #url.id#
      </cfquery>

      <!--- END GENERIC QUERIES AND VARIABLE SETTING --->

      <cfform action="" method="post">
      <table border="0" cellpadding="0" cellspacing="0">
      <tr>
                        <td width="25%">&nbsp;</td>
                        <td colspan="3">
                        <table cellpadding="2" cellspacing="0" width="100%">
                        <tr>
                              <td>Please select the Lung Associations that this user is responsible for:</td>
                        </tr>
                        <tr>
                              <td>
                              <cfoutput>#list_org.org_id#</cfoutput>

                              <CF_Columns Cols="3" Records="#list.RecordCount#">
                  
                              <table border="0" cellpadding="2" cellspacing="0" width="100%">
                              <tr>
                              <!--- Loop through the number of columns desired. --->
                              <cfloop index="LoopCount" from="1" to="3">
                                    <!--- Access the start and end variables created by the custom tag. --->
                                    <cfset #start# = ("start" & #LoopCount#)>
                                    <cfset #end# = ("end" & #LoopCount#)>
                                    <td valign="top">
                                    <cfoutput query="list" startrow="#Evaluate(start)#" maxrows="#Evaluate(end)#">
                                          #list_org.id#
                                          <cfinput type="checkbox" value="#list.id#" name="org" /> #org_abbr#<br>
                                    </cfoutput>
                                    </td>
                              </cfloop>
                              </tr>
                              </table>

                              </td>
                        </tr>
                        </table>
                        <br />
                        </td>
      </tr>
      <tr>
            <td width="25%">&nbsp;</td>
            <td colspan="3">
            <cfoutput><input type="hidden" name="users_id" value="#url.id#"></cfoutput>
            <input type="hidden" name="action" value="add">
            <input type="submit" name="Submit">
            </td>
      </tr>
      </table>
      </cfform>
0
 
anandkpCommented:
hi replace ur code with this & let me know


<CFIF IsDefined('form.action')>
      <CFIF form.action eq 'add'>
            <CFIF IsDefined("form.org") AND ListLen(form.org) GT 0>
                  <CFQUERY name="addorg" datasource="#DSN#"><!--- on add/edit - delte the previous rec from the table --->
                  Delete org_admin
                where users_id = #form.users_id#                              
            </CFQUERY>
            <CFLOOP index="i" from="1" to="#listlen(org)#>
                        <CFQUERY name="addorg" datasource="#DSN#"><!--- insert the new selection --->
                              INSERT INTO org_admin
                              ( users_id,org_id )
                              VALUES
                    (#form.users_id#,'#listgetat(org,i)#')
                        </CFQUERY>
                  </CFLOOP>
            </CFIF>
      </CFIF>
      <CFIF form.action eq 'update'>
      </CFIF>
</CFIF>

<!--- ========================================================================================================== --->

      <!--- GENERIC QUERIES AND VARIABLE SETTING --->
<CFQUERY name="list" datasource="#DSN#">
      SELECT      *
      FROM      organizations
      ORDER BY org_abbr
</CFQUERY>
<CFQUERY name="list_org" datasource="#DSN#">
      SELECT      *
      FROM      org_admin
      WHERE      users_id = #url.id#
</CFQUERY>

<!--- to show selected checkboxes - read the users already selected values --->
<CFSET userorg = #valuelist(list_org.org_id)#>

<!--- ========================================================================================================== --->
<!--- END GENERIC QUERIES AND VARIABLE SETTING --->
<CFFORM action="" method="post">
<TABLE border="0" cellpadding="0" cellspacing="0">
      <TR>
            <TD width="25%">&nbsp;</TD>
            <TD colspan="3">
            <TABLE cellpadding="2" cellspacing="0" width="100%">
                  <TR>
                        <TD>Please select the Lung Associations that this user is responsible for:</TD>
                  </TR>
                  <TR>
                        <TD>
                        <CFOUTPUT>
                              #list_org.org_id#
                        </CFOUTPUT>
                        <CF_COLUMNS Cols="3" Records="#list.RecordCount#">
                        <TABLE border="0" cellpadding="2" cellspacing="0" width="100%">
                              <TR>                                    <!--- Loop through the number of columns desired. --->
                                    <CFLOOP index="LoopCount" from="1" to="3">
                                          <!--- Access the start and end variables created by the custom tag. --->
                                          <CFSET #start# = ("start" & #LoopCount#)>
                                          <CFSET #end# = ("end" & #LoopCount#)>
                                          <TD valign="top">
                                          <CFOUTPUT query="list" startrow="#Evaluate(start)#" maxrows="#Evaluate(end)#">
                                                #list_org.id#
                                                <CFINPUT type="checkbox" value="#list.id#" name="org"/
                                <cfif listfindnocase(userorg,loopcount) neq 0>
                                    checked
                                </CFIF>
                                >
                                #org_abbr#<BR>
                                          </CFOUTPUT>
</TD>
                                    </CFLOOP>
                              </TR>
                        </TABLE></TD>
                  </TR>
            </TABLE><BR/></TD>
      </TR>
      <TR>
            <TD width="25%">&nbsp;</TD>
            <TD colspan="3">
            <CFOUTPUT>
                  <INPUT type="hidden" name="users_id" value="#url.id#">
            </CFOUTPUT>
            <INPUT type="hidden" name="action" value="add">
            <INPUT type="submit" name="Submit"></TD>
      </TR>
</TABLE></CFFORM>

<!--- ========================================================================================================== --->

K'Rgds
Anand
0
 
crosenblumCommented:
Exactly treating your form elements as a list, gives you much more power and capability.

0
 
mrichmonCommented:
No comment has been added lately, so it's time to clean up this question.
I will leave the following recommendation in the Cleanup topic area:

Accept anandkp

Please leave any comments here within the next four days.

mrichmon
EE Cleanup Volunteer
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now