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

Dynamic CFInput Names Using Arrays

Engaged ,
Oct 30, 2006 Oct 30, 2006

Copy link to clipboard

Copied

I am trying to assign text input to array values. To do this I use the below code. When I try to run the page I get the error message: "The element at position 16 in dimension 1 of object "reasonArray" cannot be found. That dimension of the object is empty. Please, modify the index expression." The elements do not go in order as the order is based off of a different dynamic part of the form.

My question: of course that position is empty, I'm trying to get it filled! Why is it giving me an error?

Thanks!
TOPICS
Advanced techniques

Views

931

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

Explorer , Oct 30, 2006 Oct 30, 2006
OK, so the array serves no purpose on the form page, since you don't yet have any data. You need that on the action page.

Hopefully this will give you some pointers...

Form Page

<Cfoutput query="getReports">
<input type="text" name="data_#getReports.report_ID#">
<inputy type="hidden" name="report_ids" value="#report_id#">
</cfoutput>

Action Page
<Cfloop list="#report_ids#" index="report_id">
<cfset thisReportData=form["data_"&report_id]>
<cfoutput><br>#report_id# = #thisReportData#</c...

Votes

Translate

Translate
Explorer ,
Oct 30, 2006 Oct 30, 2006

Copy link to clipboard

Copied

At this point you haven't actually got any array values set. What you're doing is creating a form field with the name parameter set to #reasonArray[report_id]#". This array has not been populated with anything, so it's failing.

What are you trying to achieve?

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
Engaged ,
Oct 30, 2006 Oct 30, 2006

Copy link to clipboard

Copied

I'm trying to set the array index equal to the value entered in the textbox. Later I need to send an e-mail that goes through each report id, sees if it's been selected, and if it has, lists the reason for that report.

Please let me know if this still isn't clear.

Thank you!

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
Explorer ,
Oct 30, 2006 Oct 30, 2006

Copy link to clipboard

Copied

OK, so the array serves no purpose on the form page, since you don't yet have any data. You need that on the action page.

Hopefully this will give you some pointers...

Form Page

<Cfoutput query="getReports">
<input type="text" name="data_#getReports.report_ID#">
<inputy type="hidden" name="report_ids" value="#report_id#">
</cfoutput>

Action Page
<Cfloop list="#report_ids#" index="report_id">
<cfset thisReportData=form["data_"&report_id]>
<cfoutput><br>#report_id# = #thisReportData#</cfoutput>
</cfloop>

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
Engaged ,
Oct 30, 2006 Oct 30, 2006

Copy link to clipboard

Copied

Thank you so much! That was actually my backup solution that I've been using, but it doesn't work without the form[] so I've been writing everything out, which defeats the whole purpose of dynamic code. Do you know where I can find documentation on that part?

Also, jdeline, thanks for your comment about the logic error. I edited a lot of the unnecessary code to make the post more readable and turns out it was just more confusing. Thanks though!

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
Guest
Oct 30, 2006 Oct 30, 2006

Copy link to clipboard

Copied

In my experience, you must directly populate an array element with an assignment. You might try the following: name="R#report_id#" In your action page, you can easiely assign the Rnnn to the reasonArray, using nnn as the index into the array.

BTW, you have a logic error in the first part of your code. If the <CFIF> statement is FALSE, you do not get the <TR tag, and style="font-size:11;"> is hanging out.

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
Explorer ,
Oct 30, 2006 Oct 30, 2006

Copy link to clipboard

Copied

LATEST
Honestly I'm not sure about the documentation part.

Basically, there's no difference between: -
form.my_field and form["my_field"]

They both reference the same value. But the array style notation is much more versatile and totally dynamic (plus avoids using evaluate - which is what we had to do many years ago!)

You can do the same thing with all the variable scopes

session["myStructure"].myValue

Use cfdump to see more, for example
<cfdump var="#server#">
<cfdump var="#variables#">
<cfdump var="#form#">

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