Skip to main content
Known Participant
May 19, 2011
Answered

Please help with dynamic form field names in cfloop

  • May 19, 2011
  • 2 replies
  • 1241 views

Hi,

I need to create a form with day, date, month, year and time for 12 months

Instead of coding them 12 times, I create day,date,month,year and time fileds in my form 1 time and then I use cfloop from="1" to="12" index="i" to loop these form fields 12 times.

I gave each form field name such as: <input type="text" name="ScheduleDate_# i #" value=" "> this way each of those field will be named differently such as:

ScheduleDate_1, ScheduleDate_2,ScheduleDate_3, ScheduleTime_1,ScheduleTime_2, etc

I'm facing problem when this form is submitted, I think the error has something to do with the pound sign (##) when it comes to updating the back end

I use MS SQL

It doesn like this:

<CFLOOP From="1" To="12" index="k">

<CFIF Len(Trim(Form.MeetYear_#k#)) NEQ 0 AND Len(Trim(Form.MeetTime_#k#)) NEQ 0>

<cfquery name="CreateSchedule" datasource="#DSN#">

UPDATE TableSchedule

SET SchedDay = <cfqueryparam cfsqltype="cf_sql_varchar" value="#form.SchedDay_#k##">,

SchedMonth =

<cfqueryparam cfsqltype="cf_sql_varchar" value="#form.SchedMonth_#k##">,

SchedDate =

<cfqueryparam cfsqltype="cf_sql_varchar" value="#form.SchedDate_#k##">,

SchedYear =

<cfqueryparam cfsqltype="cf_sql_varchar" value="#form.SchedYear_#k##">,

SchedTime =

<cfqueryparam cfsqltype="cf_sql_varchar" value="#form.SchedTime_#k##">

Where SchedId = <cfqueryparam cfsqltype="cf_sql_numeric" value="#k#">

</cfquery>

</CFIF>

</CFLOOP>

Can anyone help please?

This topic has been closed for replies.
Correct answer Dave Watts

You can't nest hash marks. Do this instead:

SET SchedDay = <cfqueryparam cfsqltype="cf_sql_varchar" value="#form["SchedDay_" & k]#">

Dave Watts, CTO, Fig Leaf Software

http://www.figleaf.com/

http://training.figleaf.com/

2 replies

Inspiring
May 19, 2011

In addition to Dave's answer, you would probably be better off with a single datetime field in your tableschedule table.  If necessary, you can use database functions to get whatever date parts you need.

Dave WattsCommunity ExpertCorrect answer
Community Expert
May 19, 2011

You can't nest hash marks. Do this instead:

SET SchedDay = <cfqueryparam cfsqltype="cf_sql_varchar" value="#form["SchedDay_" & k]#">

Dave Watts, CTO, Fig Leaf Software

http://www.figleaf.com/

http://training.figleaf.com/

Dave Watts, Eidolon LLC
aleckenAuthor
Known Participant
May 20, 2011

Thank you Dave! it work.

@Dan: in this case I can't use system date but thank you for the suggestion.