#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    221
    Rep Power
    11

    Exclamation Using Form Arrays in CFMX


    Hello,

    I'm currently trying to retrive an array from a form in CFMX, but am having a few problems. My form has 11 fields, all named 'newentry[]'. So once the array in the form is created, the action does the following:

    Code:
    	<cfset reginfo = "#form.newentry#(1)">
    	<cfset error = arrayNew(1)>
    	
    	<!--- Search for Blank Fields --->
    	<cfloop index="x" from="1" to="11">
    	<cfif len(trim(reginfo[x]))>
    	 <cfset reginfo[x] = "#HTMLEditFormat(reginfo[x])#">
    	 <cfset error[x] = "no">
    	<cfelse>
                  <cfset error[x] = "yes">
    	</cfif>	
    	</cfloop>
    	
    	<cfloop index="x" from="1" to="11">
    	<cfoutput>#reginfo[x]#<p></cfoutput>
    	</cfloop>
    Now, this SHOULD set the array from the form equal to a new array called "reginfo", and then it should check to see if each one is blank, before formatting it and printing out the final results. But its telling me that form.newentry is a part of some java class, and it wont let me do anything.

    Is there a special method of formatting arrays from a form that I am unaware of? Thanks!
  2. #2
  3. No Profile Picture
    Moderator

    Join Date
    Jun 2002
    Location
    Raleigh, NC
    Posts
    5,273
    Rep Power
    968
    What exactly are you trying to do? If you have 11 fields all named the same thing, they are not treated as an array on the target page. What I would recommend is naming each field something like "newEntry1", "newEntry2", etc.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    221
    Rep Power
    11
    in a form you can name something "variable[]" in order to create an array "$variable". the brackets in the form name signify 0, 1, 2, 3 in order that they appear on the form.

    I figured this would be the same for cold fusion since it works perfectly in PHP, but i cant get it to work properly.
  6. #4
  7. No Profile Picture
    Moderator

    Join Date
    Jun 2002
    Location
    Raleigh, NC
    Posts
    5,273
    Rep Power
    968
    In PHP, form elements are treated as an array. In CF, form elements are treated as a structure (key-value pairs). So if you want to have multiple forms named the same thing, like field1, field 2, etc...that's fine. But you reference it on the target page as "form.field1", "form.field2", etc. You can also reference it with this type of syntax to specify the key: form['field1']

    hope that helps.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Location
    Silver Spring, MD
    Posts
    12
    Rep Power
    0
    Or if you want all the form fields to be named exactly the same, you will get a list of values!

    For example:

    <input type="checkbox" name="state" value="MD" />
    <input type="checkbox" name="state" value="PA" />
    <input type="checkbox" name="state" value="NY" />

    If a user checked all 3 and submitted the form, form['state'] would be equal to "MD,PA,NY".

    Not sure if this applies, but it sounds like it may.

    Mike
  10. #6
  11. Wickedwd.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    wickedwd.com
    Posts
    201
    Rep Power
    0

    Thumbs up How to display array from form in coldfusion


    Assuming the form field bringing the array from the html form is called "newentry" then the code to display the array would be:

    Code:
    <cfloop index="i" list="#FORM['newentry[]']#">
    #i#
    </cfloop>
    What? I'm answering this question 7yrs. after it was asked? Well i had the same problem, i'm sure somebody else will again down the line
  12. #7
  13. No Profile Picture
    Moderator

    Join Date
    Jun 2002
    Location
    Raleigh, NC
    Posts
    5,273
    Rep Power
    968
    Or use my Form Utilities CFC, which automatically converts implicit arrays and structures into actual arrays and structures.

    http://formutils.riaforge.org/

IMN logo majestic logo threadwatch logo seochat tools logo