View comments | RSS feed
Contents > CFML Reference > Reserved Words and Variables > Scope-specific built-in variables > Form variable PreviousNext

Form variable

ColdFusion supports the Form variable FieldNames. FieldNames returns the names of the fields on a form. You use it on the action page associated with a form, as follows:

Form.FieldNames

Contents > CFML Reference > Reserved Words and Variables > Scope-specific built-in variables > Form variable PreviousNext

ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | Bug Reporting

Version 6.1

Comments are no longer accepted for ColdFusion MX 6.1. ColdFusion 8 is the current version.

Comments


No screen name said on Oct 21, 2003 at 12:48 PM :
Are the form variables in an array of some sort? Similar to in PHP with the $_POST array so you can loop through all the form variables? If not how can you abstract things some so you can submit variables without knowing how many forms there are or what they pertain to? (like form.name[x] form.address[x].... I think I might have just answered my question... but still is there a way for cold fusion to handle this type of thing so you don't have to have a bunch of input tags with names like name[]?)
jrunrandy said on Oct 21, 2003 at 1:03 PM :
Here's the answer, although I'm not sure it's what you're looking for:

Form variables are not in an Array in CF, but instead in a structure. There is also a list, #form.fieldname# of all form variables, so you can access the form variables by name, not position. Such as form.email, or form.fname.

There is also a list in the form scope that contains all of the form variables in the scope.

Here are a few different ways to get form variables dynamically:
------------------
Cfdump: if you just want to see what was returned.
<cfdump var="#form#">

CFLOOP Collection:
<cfloop item="key" collection="#form#">
#key# - #form[key]#<br>
</cfloop>

CFLOOP list:
<cfloop item="x" list="#form.fieldname#">
#x# - #form[x]#<br>
</cfloop>
No screen name said on Jan 3, 2004 at 4:20 PM :
Thanks for the answer, although it really doesn't address the issue I was having (it did help though ;). I want to have a group of forms such as a group of check boxes where there is not a set limit as to how many will be selected. I managed to solve this issue using some javascript to loop through the checkboxes and then create a list that cold fusion could then put in an array. I am sure this is not the preferred way to do it. PHP works the same way as the javascript did. I am curious as to how cold fusion is supposed to handle this situation since while solution works I don't believe it is very clean since it relies on javascript.
iJames said on Feb 9, 2004 at 3:55 PM :
The array returned in PHP I think is found in the comma separated list returned as you can see in the dump and the cfloop (needing a <cfoutput> to work though).

Isn't that what you're looking for? The array of form elements named identically?
halL said on Feb 10, 2004 at 6:29 AM :
The following example shows how to do this.
It has three checkboxes with the same name, color, and different values.
When you submit the form, all the values of the selected color boxes are in a comma-delimited list in the Form scope with the variable name color.
You can loop over them using a cfloop with a list attribute.

<form action="#CGI.SCRIPT_NAME#" method="post">
Red: <input type="checkbox" name="color" value="red" checked> <br>
Blue: <input type="checkbox" name="color" value="blue"> <br>
Green: <input type="checkbox" name="color" value="green" checked><br>
<input type="Submit">
</form>

<cfif IsDefined("Form.color")>
A dump of the Form scope<br>
<cfdump var="#Form#"><br><br>
Looping over the list<br>
<cfoutput>
<cfloop index="item" list="#form.color#">
#item#<br>
</cfloop><br>
</cfoutput>
</cfif>
scubaholic said on Feb 23, 2004 at 8:28 AM :
Having just tried this, correct syntax for getting at form fieldnames through the list is:

<cfloop index="x" list="#form.fieldnames#">
<cfoutput>
#x# - #form[x]#<br></cfoutput>
</cfloop>
bizarrojack said on Sep 15, 2004 at 12:35 PM :
Warning: FORM.fieldnames does not list all field names!!
A better name for this variable might have been "FORM.most_fieldnames", because any form variable ending with _date or _time will be left out, for some reason.
bizarrojack said on Sep 15, 2004 at 1:37 PM :
http://livedocs.macromedia.com/coldfusion/6.1/htmldocs/format28.htm#wp1135213

Here is the complete list of reserved suffixes that won't show up in FORM.fieldnames and the explanation of what they are reserved for. News to me.
ShawnPO said on Jan 31, 2005 at 12:55 PM :
Yes, but those fields are for validation only and are hidden fields. They really wouldn't serve much purpose on the process page. But I do agree, to say it lists ALL form fields is a bit misleading.
thunderJ said on Aug 10, 2005 at 6:20 PM :
I have a form like the following. The point of it is to be able to edit multiple records at the same time on the same form. I can not read the varibles that uses the format [ ]. Is there a way to read them as a array without usesing the Form.FieldNames? I get the error: You have attempted to dereference a scalar variable of type class java.lang.String as a structure with members.

-------------------------- form.cfm --------------------------

<FORM action="somepage.cfm" method="post">

<CFSET index = 0>
<CFOUTPUT query="myQuery">
<INPUT type="hidden" name="ID[#index#]" value="#ID#">
<INPUT type="text" name="FNAME[#index#]" value="#FNAME#">
<INPUT type="text" name="LAST[#index#]" value="#LNAME#">
<CFSET index = index + 1>
</CFOUTPUT>

<CFOUTPUT>
<INPUT type="hidden" name="INDEX" value="#index - 1#">
</CFOUTPUT>

<INPUT type="submit" name=""
</FORM>

-------------------------- somepage.cfm --------------------------

<CFOUTPUT>
<CFLOOP index="i" from="0" to="#FORM.INDEX#" step="1">
#FORM.ID[i]#
</CFLOOP>
</CFOUTPUT>

---------------------------- test.cfm ----------------------------
It seems like there should be a way to do it because the following lets me view the data.

<CFLOOP index="x" list="#FORM.FIELDNAMES#">
#x# - #FORM[x]#<BR>
</CFLOOP>

I guess I could parse the FORM.FIELDNAMES for the ID and FNAME, LNAME fields, but who likes parsing?

Any input?
thunderJ said on Aug 10, 2005 at 7:30 PM :
I figured it out!!! I needed to add " ".

<CFLOOP index="i" from="0" to="#FORM.INDEX#" step="1">
<CFSET ID = FORM["ID[#i#]"]>
<CFSET OLD = FORM["FNAME[#i#]"]>
<CFSET NEW = FORM["LNAME[#i#]"]>
<CFQUERY datasource="DSN">
UPDATE TABLE
SET FNAME = '#FNAME#',
LNAME = '#LNAME#'
WHERE ID = ID
</CFQUERY>
</CFLOOP>
thunderJ said on Aug 10, 2005 at 7:40 PM :
SORRY, FIXED OLD & NEW VAR!

<CFLOOP index="i" from="0" to="#FORM.INDEX#" step="1">
<CFSET ID = FORM["ID[#i#]"]>
<CFSET FNAME= FORM["FNAME[#i#]"]>
<CFSET LNAME= FORM["LNAME[#i#]"]>
<CFQUERY datasource="DSN">
UPDATE TABLE
SET FNAME = '#FNAME#',
LNAME = '#LNAME#'
WHERE ID = ID
</CFQUERY>
</CFLOOP>

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/6.1/htmldocs/expres12.htm