Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need help with dynamic form field names

Status
Not open for further replies.

PushCode

Programmer
Dec 17, 2003
573
US
I am trying to insert some records based on a submitted form. The form page will submit multiple records to an action page. The action page will then do a loop for each record submitted, and insert some info into the DB. My problem is that one of the fields on the form has a dynamic name.

I'm not sure how to loop through the results and get the appropriate form field name for each record.

The field in question on the form is the one with name="#contact_id#". On the action page, the values from that field should be inserted as the 'sent_code' value.

Anyone know how to do this? Thanks in advance.

Form page code:
<cfif ...>
<input type=&quot;hidden&quot; name=&quot;#contact_id#&quot; value=&quot;1&quot;>
<cfelseif ...>
<input type=&quot;hidden&quot; name=&quot;#contact_id#&quot; value=&quot;2&quot;>
<cfelseif ...>
<input type=&quot;hidden&quot; name=&quot;#contact_id#&quot; value=&quot;3&quot;>
<cfelseif ...>
<input type=&quot;hidden&quot; name=&quot;#contact_id#&quot; value=&quot;4&quot;>
</cfif>
<input type=&quot;hidden&quot; name=&quot;contact_id&quot; value=&quot;#contact_id#&quot;>
<input type=&quot;submit&quot; value=&quot; Submit &quot; name=&quot;submit_form&quot;>

Action page code:
<cfset contact_list = form.contact_id>

<cfloop list=&quot;#contact_list#&quot; index=&quot;id&quot;>
<cfquery name=&quot;insert_it&quot; datasource=&quot;#ds#&quot;>
INSERT INTO sri_contact.dbo.contact_cob_sent_tracker(contact_id, sent_code, date_sent)
VALUES(#id#, #sent_code#, #DateFormat((Now()), &quot;mm/dd/yyyy&quot;)#)
</cfquery>
</cfloop>
 
PushCode, try putting poundsigns around form.contact_id in the <cfset>. Like <cfset contact_list = #form.contact_id#>, and if that doesn't work then try <cfset contact_list = '#form.contact_id#'>

The latter one has single quotes around the variable name and the poundsigns.

Let us know if you get any errors.

[sub]
____________________________________
Just Imagine.
[sub]
 
I may not have been clear enough...

If you notice on the form code, there is a hidden field called contact_id with the dynamic VALUE of #contact_id#.
...that is what form.contact_id is referring to on the action page. It is fine, and not the problem here.

The other field on the form code has a dynamic NAME of #contact_id#. It's that field that I'm having trouble referencing so that I can insert the appropriate corresponding value (1,2,3 or 4) into the #sent_code# field of the table for each record submitted.

Does that make more sense? This is trick to explain.
 
I thought that putting this code:
<cfset sent_code = 'form.' & #id#>

...right before the insert query would do it, but no luck.
 
So you're saying that this form field is your problem:
Code:
<input type=&quot;hidden&quot; name=&quot;#contact_id#&quot; value=&quot;1&quot;>
I see that you have several of these seperated out by cfif tags. What are your &quot;if&quot; conditions? What are you using to determine which hidden value is used? Without knowing that, I have to say that this form looks backwards, I've never really seen anyone try to hard code a static value to a dynamic name, it's usually the other way around.

Try this, rename all of the hidden form fields &quot;sent_code&quot;, and use your IF statements to determine which one is used (like you currently have set up).
Code:
<input type=&quot;hidden&quot; name=&quot;sent_code&quot; value=&quot;1&quot;>
Then, in your INSERT query, just reference &quot;#Form.sent_code#&quot; for the value that your IF statements selected.

If this isn't what you're trying to do, you'll probably need to show a little more code so we can get a better idea of what's going on.





Hope This Helps!

Ecobb

&quot;My work is a game, a very serious game.&quot; - M.C. Escher
 
Why not set a hidden field at the bottom of the form that carries a comma-delimited list of the formfield names which you could set up like:

<!--- submitting_page.cfm --->
<!--- first create and empty value --->
<cfset fieldnamelist = "">
<!--- next, loop through your code that creates the dynamic fieldname and append to it "fieldnamelist" as they are created, separating by commas AFTER the first name is added to list --->
<form action="action.cfm" method="post">
<cfloop>
<dynamicfieldname = "code that generates fieldname">
<input name=#dynamicfieldname# value="">
<cfscript>
if (fieldnamelist = '') {
fieldnamelist = dynamicfieldname;
}
else {
fieldnamelist = fieldnamelist & ',' & dynamicfieldname;
}
</cfscript>
</cfloop>
<input type"hidden" name="theFieldNames" value="#fieldnamelist#">
<input type="submit" value="submit">
</form>
<!--- now on the action template, loop through form.theFieldNames, parsing it into form values --->
<!--- action_template.cfm --->
<cfoutput>
<cfif IsDefined("form.theFieldNames") and form.theFieldNames is not ''>
<cfloop list="#form.theFieldNames#" delimiters="," index="thisNameValue">
<cfscript>
temp = 'form.' & thisNameValue;//this sets up the NAME of the variable
temp = evaluate(temp);//this gets the VALUE of the dynamic formfield
ValueToUse = temp;//rename the variable to whatever you need in the code that uses the value, if necessary
</cfscript>
<!--- now execute the code that will use the VALUE of form.whatever that is parsed out here, then continue the loop to get the next fieldname from the list "form.theFieldNames" --->
#ValueToUse#<br>
</cfloop>
</cfif>
</cfoutput>

Try it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top