Go Back   CodingForums.com > :: Server side development > Other server side languages/ issues > ColdFusion

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rating: Thread Rating: 18 votes, 5.00 average.
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-03-2009, 03:19 PM   PM User | #1
Rick7707
New to the CF scene

 
Join Date: Dec 2008
Location: Houston, TX
Posts: 7
Thanks: 2
Thanked 0 Times in 0 Posts
Rick7707 is an unknown quantity at this point
Uploading multiple files

I keep getting this error: Element NUMBEROFFIELDS is undefined in VARIABLES

If anyone could help I would appreciate it.

Here is the client side:
<cfset numberoffields = 4>
<form action="fileupload.cfm" method="post" enctype="multipart/form-data">
<cfloop index="i" from="1" to="#numberoffields#" step="1">
<cfset filename = "file" & #i#>

<input type="file" name="<cfoutput>#filename#</cfoutput>" /><br />

</cfloop>

<input type="submit" name="submit" value="Upload">
</form>

Here is the server side:
<cfloop index="i" from="1" to="#variables.numberoffields#" step="1">
<cfset filename = "form.file" & #i#>

<cfif evaluate(variables.filename) NEQ "">

<cffile action="upload" filefield="#variables.filename#" destination="d:\bla\bla\bla" accept="image/jpg, image/png, image/x-png, application/msword, application/vnd.ms-word, application/mspowerpoint, application/vnd.ms-powerpoint, application/pdf, application/msexcel, application/vnd.ms-excel" nameconflict="makeunique">
</cfif>
</cfloop>
Rick7707 is offline   Reply With Quote
Old 03-03-2009, 03:36 PM   PM User | #2
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,680
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
Please follow http://www.codingforums.com/postguide.htm
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Old 03-05-2009, 06:14 AM   PM User | #3
Gjslick
Regular Coder

 
Join Date: Feb 2009
Location: NJ, USA
Posts: 476
Thanks: 2
Thanked 70 Times in 69 Posts
Gjslick will become famous soon enough
Hey Rick. What the problem is, is that you're not actually sending the numberOfFields variable in the form, and therefore it's not getting to the next page (fileUpload.cfm). Your best option is to send numberOfFields in a hidden form field like this:

Code:
<cfset numberOfFields = 4>
<form action="fileUpload.cfm" method="post" enctype="multipart/form-data">
  <input type="hidden" name="numberOfFields" value="#numberOfFields#">

  <cfloop index="i" from="1" to="#numberoffields#" step="1">
     <cfset filename = "file" & #i#>

     <input type="file" name="<cfoutput>#filename#</cfoutput>" /><br />
   </cfloop>

  <input type="submit" name="submit" value="Upload">
</form>
The only variables that the fileUpload.cfm page are going to receive are the variables from the form's submission (and URL variables, if you sent any). That is, it doesn't matter what variables you set with <cfset> on the form's page, they have no way of finding themselves to the fileUpload.cfm page when the form is submitted unless they are actually a form field.

Now on your fileUpload.cfm page, variables that are coming from the form should be referenced in the form scope. You have the filename one correct, but your loop should be:
Code:
<cfloop index="i" from="1" to="#form.numberOfFields#">
(By the way, you don't need to specify the step="1" attribute, as 1 is the default step.)


Real quick just to let you know, any document with ColdFusion code in it is really "server side" code. ColdFusion code is always run at the server, and then the user's browser just receives the resulting HTML page that is created by running that code. So even tho your code looked like this in the .cfm file for the code that you labeled as the "client side" code:
Code:
<cfset numberOfFields = 4>
<form action="fileUpload.cfm" method="post" enctype="multipart/form-data">
  <cfloop index="i" from="1" to="#numberoffields#" step="1">
     <cfset filename = "file" & #i#>

     <input type="file" name="<cfoutput>#filename#</cfoutput>" /><br />
   </cfloop>

  <input type="submit" name="submit" value="Upload">
</form>
The code that the client's browser actually recieved is:
Code:
<form action="fileupload.cfm" method="post" enctype="multipart/form-data">
  <input type="file" name="file1" /><br />
  <input type="file" name="file2" /><br />
  <input type="file" name="file3" /><br />
  <input type="file" name="file4" /><br />    
  <input type="submit" name="submit" value="Upload">
</form>
You can always check what the client's browser receives by right clicking on your own browser and clicking "View Source" in IE, or "View Page Source" in Firefox.

Hope that helps, and post again if there's anything else.
Gjslick is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:43 AM.


Advertisement
Log in to turn off these ads.