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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dynamic form fields 1

Status
Not open for further replies.

boatguy

Programmer
Oct 22, 2001
153
US
Hi,

I have a client that is going to pass me form data for insertion into a dB table. The problem is, although many of the form fields are static, they will also pass several dynamic option fields in the process. All of the option field names are prefaced with option_ so I need to understand how I can identify the fields that are options and insert them into the options MS SQL dB column with any type of delimiter.

So, they will send static info (firstname, lastname, address etc) and also dynamic fields (option_seating, option_engine, option_extcolor etc).

I want my insert to be something like:
Insert into Leads (firstname, lastname, address, options)
Values('#firstname#', '#lastname#', '#address#', '#optionslist#')

Please let me know if I am not clear.

Thanks

 
<cfscript>
myOtions = StructNew();
myOptions.option_seating = 2;
myOptions.option_engine = 'F100-PW-200';
myOptions.option_extcolor= 'falcon silver';
</cfscript>

<cfset myPos = 1>
<cfset myOpts = 'delete'>
<cfif NOT StructIsEmpty(myOptions)>
<cfset keysToStruct = StructKeyArray(myOptions)>
<cfloop index="i" from="1" to="#ArrayLen(keysToStruct)#">
<cfset myOpts = ListInsertAt(myOpts, myPos, #myOptions[keysToStruct]#)>
<cfset myPos = myPos + 1>
</cfloop>
<cfset myOpts = ListDeleteAt(myOpts,listLen(myOpts))>
</cfif>
<cfdump var="#myOptions#">
<cfdump var="#myOpts#">

for the db insert,

Insert into Leads (firstname, lastname, address, options)
Values('#firstname#', '#lastname#', '#address#', '#myOpts#')


hope it helps...


 
I'm not sure how the above example will help, but if it does, great!

I thought I read that they are sending you form data.

So you will need to loop through the form structure and find all fieldnames that begin with option_

Here is a sample that you can copy and paste into a .cfm file and see how it might work

Code:
<form action="" method="post">
	d1. <input type="text" name="option_seats" /><br/>
	d2. <input type="text" name="option_color" /><br/>
	d3. <input type="text" name="option_engine" /><br/>
	d4. <input type="text" name="option_wheels" /><br/>
	d5. <input type="text" name="option_something_else" /><hr/>
	
	s1. <input type="text" name="fname" /><br/>
	s2. <input type="text" name="lname" /><br/>	
	s3. <input type="text" name="postal_code" /><br/>	
	<input type="submit" name="sendForm" value="Go">
</form>

<cfif structKeyExists(form,"sendForm")>
<!--- set up the blank list --->
<cfset optionList = "">
<!--- cfoutput to see whats going on, no need in production version --->
<cfoutput>
	<!--- loop the entire form with the collection attribute. I will be the form field Name, #form[i]# will give you it's value --->
	<cfloop collection="#form#" item="i">
		<!--- check if it is a option field on each loop. I thought it might be easiest to use listfirst on the form field name with the underscore as the delimiter. so if the first element is 'option' then you know its an option field. --->
		<cfif listFirst(i,"_") IS "option">
			<!--- quick output of the form field name and it's value for example you would use this in the query #form[i]# --->
			<b>#i# = #form[i]#</b><br/>
			<!--- add to optionList --->				
			<cfset optionList = listAppend(optionList,form[i])>
		</cfif>
	</cfloop>
<!--- output the optionList to see whats happening ---->
optionlist = #optionlist#
</cfoutput>
<!--- your query then goes here... --->
</cfif>

since you will not be the one providing the form, you will need to mainly focus on the lower section, inside of the cfif structkeyexists block... but all together it's an example you can test with on your own.

Kevin

Phase 1: Read the CFML Reference
Phase 2: ???
Phase 3: Profit!
 
Thanks guys.

imstillatwork it working like a charm.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top