• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Problem/Question Regarding Form Variables and Nesting?

New Here ,
Mar 29, 2016 Mar 29, 2016

Copy link to clipboard

Copied

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?

Views

364

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Advocate , Mar 29, 2016 Mar 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

Votes

Translate

Translate
Advocate ,
Mar 29, 2016 Mar 29, 2016

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 29, 2016 Mar 29, 2016

Copy link to clipboard

Copied

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,

^_^

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 29, 2016 Mar 29, 2016

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Mar 29, 2016 Mar 29, 2016

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 29, 2016 Mar 29, 2016

Copy link to clipboard

Copied

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>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Mar 29, 2016 Mar 29, 2016

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 30, 2016 Mar 30, 2016

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Mar 30, 2016 Mar 30, 2016

Copy link to clipboard

Copied

LATEST

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

Cheers

Eddie

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation