Skip to main content
Known Participant
March 29, 2016
Answered

Problem/Question Regarding Form Variables and Nesting?

  • March 29, 2016
  • 3 replies
  • 618 views

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?

    This topic has been closed for replies.
    Correct answer EddieLotter

    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

    3 replies

    pkonshakAuthor
    Known Participant
    March 29, 2016

    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.

    EddieLotter
    EddieLotterCorrect answer
    Inspiring
    March 29, 2016

    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

    WolfShade
    Legend
    March 29, 2016

    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,

    ^_^

    EddieLotter
    Inspiring
    March 29, 2016

    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