8 Replies Latest reply on Mar 30, 2016 10:27 AM by EddieLotter

    Problem/Question Regarding Form Variables and Nesting?

    pkonshak

      Hello,

       

      I need to create a rather long list of checkboxes for a user to fill out.  I have created a database table with each checkbox item stored as a row, and using a cfquery I can select them from the database and add them to a form as such:

       

      <form action="postact.cfm" method="post">

          

           <cfoutput query="getquestions">

                 

                  <input type="checkbox" name=#getquestions.shortname#>#getquestions.longname#<br>

       

            </cfoutput>

          

        <input type="submit" value="Submit">

       

      </form>

       

      The problem is when I try to reference these items on the following page.

       

      Instead of naming each one individually, I was hoping to use another query to pull the name of the checkbox, and be able to reference it.

       

      Problem seems to be that I can't reference them as a variable name, I thought I could do something like this:

       

      <cfoutput query="getquestions">

      #form.shortname#

      </cfoutput>

       

      ..but that doesn't work because #form.shortname# isn't a valid variable.  What I really need is #form.#getquestions.shortname##, if that makes sense.  Is that possible?  Nesting a variable name inside a variable name?

        • 1. Re: Problem/Question Regarding Form Variables and Nesting?
          EddieLotter Level 3

          I suspect what you're trying to do is the following:

          <cfoutput query="getquestions">
          <cfif isDefined("form." & shortname)>
            <!--- Process form field. --->
          </cfif>
          </cfoutput>
          
          

           

          Cheers

          Eddie

          • 2. Re: Problem/Question Regarding Form Variables and Nesting?
            WolfShade Level 4

            Yep.. as EddieLotter has demonstrated, a checkbox that isn't checked upon form submit does not exist, so you just check to see if the checkbox exists.

             

            That's if you want to use another query to loop through.  You could also use form.fieldnames as a comma-delimited list and use ListFind() or ListContains(); or cfloop through the list, which wouldn't need to check for the existence of a checkbox, as the list would be ONLY those that exist.

             

            HTH,

             

            ^_^

            • 3. Re: Problem/Question Regarding Form Variables and Nesting?
              pkonshak Level 1

              Let's assume I set every check box to be "off", so they are defined:

               

              <form action="postact.cfm" method="post">

                 

                   <cfoutput query="getquestions">

                        

                          <input type="hidden" name=#getquetions.shortname#  value="off">

                          <input type="checkbox" name=#getquestions.shortname#>#getquestions.longname#<br>

               

                    </cfoutput>

                 

                <input type="submit" value="Submit">

               

              </form>

               

              Now a box that has not been checked is "off".  A box that has been checked will be "on,off".  So everything is defined.

               

              I still can't reference them in the following page using a dynamic variable name generated from my cfquery.  I know I'm doing a terrible job of explaining this...

               

              I want to pull the shortname out of the db table, and display the value corresponding to that #form.shortname#..

               

              Let's say one is called height.  So on the form, there's a checkbox for height with the same shortname.  You check it.  The next page, I can reference it as:

               

              #form.height#

               

              That will work.  But, I have 27 fields and I don't want to name each one manually.  What I really want to be able to do is pull the name of each out of the database, then show the value associated with it, so:

               

              #form.#shortname## -- but that is an invalid name, because you can't have a period in a variable name.

               

              I hope this is making some more sense.

              • 4. Re: Problem/Question Regarding Form Variables and Nesting?
                EddieLotter Level 3

                As a demonstration, put the following code in postact.cfm and it will output the name and value of each form field:

                <cfoutput query="getquestions"> 
                  <cfif isDefined("form." & shortname)> 
                    form.#shortname# = #form[shortname]#<br>
                  </cfif> 
                </cfoutput>
                

                 

                Cheers

                Eddie

                • 5. Re: Problem/Question Regarding Form Variables and Nesting?
                  BKBK Adobe Community Professional & MVP

                  If I understand correctly, you want the query to be available on any arbitrary page to which you submit the form. If so, then a convenient solution is to store the query in the session.

                   

                  Do something like this on the form page:

                   

                  <cfset session.getquestions = getquestions>

                  <cfoutput query="getquestions">       

                              <input type="hidden" name=#getquestions.shortname#  value="off">

                              <input type="checkbox" name=#getquestions.shortname#>#getquestions.longname#<br>

                  </cfoutput>

                   

                  Then obtain the form variables on the action page of the form (postact.cfm) as follows:

                   

                  <cfoutput query="session.getquestions">

                  <cfif structKeyExists(form, session.getquestions.shortname)>

                  form field name: #session.getquestions.shortname#<br>

                  form field value: #form[session.getquestions.shortname]#<br>

                  </cfif>

                  </cfoutput>

                  • 6. Re: Problem/Question Regarding Form Variables and Nesting?
                    EddieLotter Level 3

                    The optimization that @BKBK is presenting can also be achieved using the cachedWithin attribute of the cfquery tag, which avoids having to lock the session scope every time you access it.

                     

                    Cheers

                    Eddie

                    • 7. Re: Problem/Question Regarding Form Variables and Nesting?
                      pkonshak Level 1

                      Thanks to everyone who answered -- Eddie's method above of bracketing the variable name worked for me!

                      • 8. Re: Problem/Question Regarding Form Variables and Nesting?
                        EddieLotter Level 3

                        I'm glad you got this resolved, however, I urge you to consider the optimizations that others have offered.

                         

                        Cheers

                        Eddie