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

Get the count of Fields of a Form 3

Status
Not open for further replies.

choudary

Programmer
Sep 3, 2000
19
ID
How can i get the count of fields of a form when i submit it so that i can loop through all the form fields instead of going though each element name by name.

And also how can i get the names of the fields of a form, say i want to have output like Name of the field and its value.
like
FirstName: XYZ
LastName: CVC
.
.
.
Etc.,

I think its a simple task, ;)
thanks
choudary
 
<cfset toto=ListToArray(#form.FIELDNAMES#)>
<!--- form.fieldnames contains all the fields of the form in a list, but i don't know how to get the size of a list, this is why i set it as an array --->
<cfset how_many_fields=#ArrayLen(toto)#>

be careful that it will count ALL the fields, even hidden fields or submit or reset buttons are counted in
 
Thanks a lot for ur help dear Iza but i also asked for how to get the names of the form fields, to have an output look like.........

Field1 : value
Field2 : value....etc.,

Thanks again

choudary
 
This will loop through all form fields and display the key-value pairs.

<table>
  <cfloop index=&quot;fname&quot; list=&quot;#form.fieldnames#&quot;>
    <cfoutput>
<tr>
<td>field name: #fname#</td>
<td>field value: #Evaluate(&quot;form.&quot; & fname)#</td>
</tr>
    </cfoutput>
  </cfloop>
</table>

Dave
 
For your information: the length of a list can be determined by #ListLen(MyList)#. However, Dave's approach is correct. <webguru>iqof188</webguru>
 
thanx iqof188 :]]

choudary : Dave's right !!!
 
My personal favourite in the Form.Feildnames category is using:

<cfloop collection=&quot;#Form#&quot; item=&quot;ThisField&quot;>
<cfoutput>
The value of #ThisField# is #Form[ThisField]#
</cfoutput>
</cfloop>


Taken to the extreme, you can build a completely dynamic query/form/output page like this:

 
When looping through the form elements as a list, you have to be aware of any commas that the user may enter as a form value. If a field in a form contains a comma, then that field will be placed in the list as two separate elements (separated by the comma).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top