Link to home
Start Free TrialLog in
Avatar of James Rodgers
James RodgersFlag for Canada

asked on

Assigning form object names to cf variables?

I have a dynamic form that populates based on an employee list. The form contains the following for each employee
textbox name trnDate#idx# value date of training
textbox name empName#idx# value employee name
hidden name empID#idx# value unique employee number
checkbox name trn#idx# value true

the employee list can be from 0 to 6000 entries i need to process the sub mission based upon the checkbox being selected. How can I assign the NAME of a form variable to a cf variable in a loop to determine the form variable is true? As in:
<cfloop index="idx" from="1" to="#var#">
   <cfif form.trn#idx#>
      <cfoutput>
         trnDate#idx#
         empName#idx#
         empID#idx#
      </cfoutput>
   </cfi>
</cfloop>

it doesn't work assignment of the form field names is what is giving me a hard time.
Avatar of jeffryan
jeffryan

How about trying this:

<cfloop index="idx" from="1" to="#var#">
  <cfif isdefined("form.trn#idx#")>
     <cfoutput>
        trnDate#idx#
        empName#idx#
        empID#idx#
     </cfoutput>
  </cfi>
</cfloop>
How about trying this:

<cfloop index="idx" from="1" to="#var#">
  <cfif isdefined("form.trn#idx#")>
     <cfoutput>
        trnDate#idx#
        empName#idx#
        empID#idx#
     </cfoutput>
  </cfi>
</cfloop>
ASKER CERTIFIED SOLUTION
Avatar of Concigliere
Concigliere

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It is much easier to use the same form name instead of dynamic form names.

Then do a listlen on the submitted form
For example your form name is employee_names

<cfif isdefined("form.fieldnames")>
<cfset emp_count = listlen(form.employee_names)>
<Cfif emp_count eq 0>
handle as if only 1 record sent.
<cfelse>
<cfloop from=1 to=#emp_count# index=x>
  <cfquery name="add_record" datasource="whatever">
    insert into employees (name)
    values ('#listgetat(form.employee_names,x)#')
  </cfquery>
</cfloop>

This technique gets more complicated with more form elements.

But it is very easily doable.

Because everything is a form, you can loop and take form data and make it into a query, via querysetcell if you had a need for sorting form data.

As I have had to do, on a few occasions.

The key is whether multiple/single form element data is sent...You use listlen to determine number of elements in the form.

Then do a listgetat(form.formname,position) to grab the data from the form.formname

I hope that helps...

Exploring The Truth
http://mindstream.blogspot.com
give this a try

<CFLOOP index="idx" from="1" to="#var#">
<CFIF isDefined('form.trn#idx#')>
          <CFOUTPUT>
               #Evaluate('trnDate#idx#')#
               #Evaluate('empName#idx#')#
               #Evaluate('empID#idx#')#
          </CFOUTPUT>
</CFIF>
</CFLOOP>

let me know

K'Rgds
Anand
I just personally dislike dynamic form names.

It has no point, when you can use common sense form names, and just pass it, and convert it into a list.

Meaning any form element that has multiple values will be like a list.

example someone typed in their first name twice in two seperate forms going to the same action page

form.firstname = craig,craig

so if you do listlen(form.firstname) will equal 2

then you can loop thru that, and if there are other expected fields to be list format.

you can do like above...

loop thru list length - use any form element it doesn't matter as long as it has equal length as the rest of the fields.


Maybe he is using dynamic form names because he is outputting a query of multiple employees in one form and wants to work with mulitple records in the one form.
dynamic form names are an old idea, when there are much better ways of handling it.

Such as having the same form name. And because each form element will have multiple values, they can be treated like list elements.

like my example did say before...

The key with data manipulation is:

a. understanding how to easily convert one kind of data to another. Because this will be useful in current and future applications.
b. visualizing the data, being able to see it in 3d, visualize how the application changes, manipulate data
c. understand the relationships between applications.

have fun!
I don’t think your quite following the situation crosenblum…(or maybe im not) but that’s besides the point here…

You don’t like dynamic form names? Fine, don’t use them…rewrite code over and over…

But its an opinion…say it once and move on but don’t try and push it onto people here that are trying to do something the right way.

Try accomplishing something like this without them….

Create a dynamic survey admin tool where the admin can log in
.…add the survey
….add multiple questions to the survey (as many as he likes) (required yes/no)
….add multiple possible answer per question (as many as he likes) (radio buttons)
….give each question the option of having and “Other” answer with a text field for specification of answers

Once it is created 3 dynamic tables are created as well

…one holds all possible questions for that particular survey
…two holds all possible answers per question of table one
…three holds all results of every submission of the current survey

how would you build the forms to get the survey to the user?
….dynamically
how would you write a validation of the fields to make sure the required questions were answered?
….dynamically
how would you capture all results for entering into the database?
….dynamically
how would you display percentage results of a dynamic survey when you have no idea what info is in the database?
….dynamically

crosenblum
“dynamic form names are an old idea, when there are much better ways of handling it.”

you may be able to do it without dynamic form names…but it will limit the application making it far from “better”, non-reusable and not resalable without drastic modification…

Dynamic = Reusable = mo money mo money


sorry if i come off as an ass

just woke up, no caffeine, and tired of being left with the job of trampling through code of developers who didnt last becuz of their static coding habits
dynamic code wld help u out with :

1. less code
2. easy maintenance
3. no overheades ie :if it works for one - it will work for other as well
4. quick debugging
5. easy understanding

depends on the way u look at it - if u try & look at the brighter side - u can find benefits in it ... its a personal opinion & everyone is free to do things - the way they wanna do.

so lets see what jeter_48 has to say abt this ... i mean the answere's posted for him !

Cheers
Anand
So would the new way, it's just easier to track down where and why a certain thing exists if it uses common sense naming convention.

I think this is the simplest and most code/server efficient method:

1) Name your check boxes all the same.
2) make the value of each checkbox the ID of the record it represents.

3) loop through the values in that form field (they will be returned as a comma delimited list).  These values are the ids you want to manilpulate.  So instead of checking every single check box to see if it was checked, you only check the ones that were.

the loop would be (if you name the checkbox idsToManipulate)

<cfloop list="#form.trn#" index="idx">
  trnDate#idx#<br>
  empName#idx#<br>
  empID#idx#<br><br>
</cfloop>

that way, you only need to change one line in your form page, the one that sets the name to trn#idx#, just call it "trn"





sorry, i meant "if you name the checkboxes "trn", not "idsToManipulate"
Avatar of James Rodgers

ASKER

There are some good comments and answers here, but I am having a hard time picking the best answer as the solution I need seems to be a selection of code snippets and comments from several people.

As for the dynamic vs static code I always use dynamic where possible. A well designed and coded dynamic function/code block can drastically reduce the need for rewrites, also designing for user requirements means anticipating what the next level of application functionality your code can accommodate, will need to accommodate and should accommodate.
An example: my first project here was to develop a dynamic reporting application that had to work until DEC 31 of the current year, on January 1 the user requirement was that with a few changes the application would work again until the end of the year, why code this? it meets the user/clients needs if it works for 1 year at a time, but why waste time and resources rewriting a few lines of code evey January when in the initial developemnt a few extra lines of code would allow the application to run forever? and what if a new developer was too green to understand the code how long would it take for that person to walk through the static code to determine where/what to change? a day, a week? how much money do you want to spend? because I can create code that you would need to rewrite on a daily basis if you are willing to pay me to do that but my time is better spent creating new DYNAMIC applications, job security isn't rewriting code you should have done right the first time, job security comes from creating excellent work and making YOU too valuable to lose!

I will select the BEST answer later today. Thanks to all for there copmments and responses.
Thanks,
While this answer is actually and expanded version of the earlier answer it meets the needs of my application more than the earlier answer.

Again thanks to all who participated in this discussion.
<quote>As for the dynamic vs static code I always use dynamic where possible. A well designed and coded dynamic function/code block can drastically reduce the need for rewrites, also designing for user requirements means anticipating what the next level of application functionality your code can accommodate, will need to accommodate and should accommodate. </quote>

well said!!

glad to help and I do appalogize for, more or less, turning your query into a debate &#9786;
gr8 going

btw - did u try out the option i had posted - just wanted to know - if it worked out for u !

K'Rgds
Anand