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

Set variables in a loop

Status
Not open for further replies.

calista

Programmer
Jan 24, 2001
545
US
Hi, folks!

I am trying to replace the following code on my form action page with a loop:
Code:
<CFIF ISDEFINED(&quot;Form.FOPEN&quot;) IS TRUE>
	<CFSET FOPEN=&quot;Yes&quot;>
<CFELSE>
	<CFSET FOPEN=&quot;No&quot;>
</CFIF>
<CFIF ISDEFINED(&quot;Form.FCLOSE&quot;) IS TRUE>
	<CFSET FCLOSE=&quot;Yes&quot;>
<CFELSE>
	<CFSET FCLOSE=&quot;No&quot;>
</CFIF>
<CFIF ISDEFINED(&quot;Form.FREAD&quot;) IS TRUE>
	<CFSET FREAD=&quot;Yes&quot;>
<CFELSE>
	<CFSET FREAD=&quot;No&quot;>
</CFIF>
<CFIF ISDEFINED(&quot;Form.FWRITE&quot;) IS TRUE>
	<CFSET FWRITE=&quot;Yes&quot;>
<CFELSE>
	<CFSET FWRITE=&quot;No&quot;>
</CFIF>
Here's what I've got so far:
Code:
<CFSET FileEventList = &quot;FOPEN,FCLOSE,FREAD,FWRITE&quot;>

<CFLOOP INDEX=&quot;EventIndex&quot; FROM=&quot;1&quot; TO=&quot;4&quot; STEP=&quot;1&quot;>
	<CFPARAM NAME=&quot;Form.#ListGetAt(FileEventList,EventIndex)#&quot; DEFAULT=&quot;False&quot;>
</CFLOOP>
<CFOUTPUT>
	<CFLOOP INDEX=&quot;EventIndex&quot; FROM=&quot;1&quot; TO=&quot;4&quot; STEP=&quot;1&quot;>
		#ListGetAt(FileEventList,EventIndex)#    #Evaluate(ListGetAt(FileEventList,EventIndex))#<BR>
	</CFLOOP>
</CFOUTPUT>
This sets an initial value correctly, but as you can see from my original code, if the box is checked, the varable S/B &quot;Yes&quot;, if it is not checked it S/B &quot;No&quot;. Later, these values are inserted into the database. I can't seem to set variables dynamically. Is what I'm trying to do possible? I hope so, because this is the easy one. It gets uglier from here.

Thanks!
Calista :-X
Jedi Knight,
Champion of the Force
 
Hey Calista,

It looks like you're attempting to solve the problem of how to deal with check boxes that aren't checked. I would suggest using radio buttons since it appears these are all yes/no type settings.

If that's not an option, I would think something like this will work (haven't tested it).

<CFSET FileEventList = &quot;FOPEN,FCLOSE,FREAD,FWRITE&quot;>
<CFLOOP INDEX=&quot;x&quot; list=#fileEventList#>
<cfset &quot;#x#&quot; = IIF( isdefined(&quot;form.#x#&quot;),DE(&quot;Yes&quot;),DE(&quot;No&quot;))>
</CFLOOP>

Hope this helps,
GJ
 
This seems of work. I give the checkbox a value of &quot;Yes&quot; on the form page, on the action page, I use CFPARAM to set all of them to &quot;No&quot;. Then I loop through the respones. If the box was checked, the form variable is set to &quot;Yes&quot;, if it was not, the form variable is set to the default, &quot;No&quot;. Here's the code for anyone who's interested:

Form Page
Code:
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>

<HTML>
<HEAD>
	<TITLE>Test Form Page</TITLE>
</HEAD>

<BODY>

<CFSET FILEEVENTLIST = &quot;FOPEN,FCLOSE,FREAD,FWRITE&quot;>

<CFOUTPUT>
<FORM ACTION=&quot;TestAction.cfm&quot; METHOD=&quot;post&quot;>
	<CFLOOP INDEX=&quot;X&quot; LIST=&quot;#FileEventList#&quot;>
		#X#   <INPUT TYPE=&quot;checkbox&quot; NAME=&quot;#X#&quot; VALUE=&quot;Yes&quot;><BR>
	</CFLOOP>
	<BR><BR>
	<INPUT TYPE=&quot;submit&quot; NAME=&quot;Submit&quot; VALUE=&quot;Submit&quot;>
</FORM>
</CFOUTPUT>

</BODY>
</HTML>
And, here is the action page:
Code:
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>
<HTML>
<HEAD>
	<TITLE>Test Action Page</TITLE>
</HEAD>
<BODY>

<CFSET FILEEVENTLIST = &quot;FOPEN,FCLOSE,FREAD,FWRITE&quot;>

<CFLOOP INDEX=&quot;EventIndex&quot; FROM=&quot;1&quot; TO=&quot;4&quot; STEP=&quot;1&quot;>
	<CFPARAM NAME=&quot;Form.#ListGetAt(FileEventList,EventIndex)#&quot; DEFAULT=&quot;No&quot;>
</CFLOOP>

<CFOUTPUT>
	<CFLOOP INDEX=&quot;EventIndex&quot; FROM=&quot;1&quot; TO=&quot;4&quot; STEP=&quot;1&quot;>
		#ListGetAt(FileEventList,EventIndex)#    #Evaluate(ListGetAt(FileEventList,EventIndex))#<BR>
	</CFLOOP>
</CFOUTPUT>
<BR><BR>
<CFOUTPUT>
	Form.FOPEN 	EQ #Form.FOPEN#<BR>
	Form.FCLOSE EQ #Form.FCLOSE#<BR>
	Form.FREAD 	EQ #Form.FREAD#<BR>
	Form.FWRITE EQ #Form.FWRITE#<BR>
</CFOUTPUT>
</BODY>
</HTML>
Calista :-X
Jedi Knight,
Champion of the Force
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top