Skip to main content
Participant
June 4, 2009
Answered

How to access the form items dynamically

  • June 4, 2009
  • 1 reply
  • 972 views

The issue here is that I want to acces the value in the textboxes dynamically

Here is the code ( I know its wrong )

<cfset temp = TaskEntryIDs.Split(',') />

    <cfloop index="x" from="1" to="#arrayLen(temp)#">
   
    <cfset TempControl = "Form.t"&temp>
    <cfif isdefined("Form.t"&temp)>
         <cfoutput>#"Form.t"&temp#</cfoutput><br/>
    </cfif>
    </cfloop>

I don't know the correct way to access it, I know that the textbox name start with T and the task number

I know that the textbox for the task number 74 is t74, but how can I access the value of this text box so I can insert it into to the database ?

Please help me

Thanks

This topic has been closed for replies.
Correct answer ilssac

Array Notation:

Form["t" & temp]

StuctKeyExists() is an easier function to determine dynamic form fields exist.

<cfif structKeyExists(form,"t" & temp)>  rather then isDefined().

1 reply

ilssac
ilssacCorrect answer
Inspiring
June 4, 2009

Array Notation:

Form["t" & temp]

StuctKeyExists() is an easier function to determine dynamic form fields exist.

<cfif structKeyExists(form,"t" & temp)>  rather then isDefined().

Participant
June 12, 2009

Here is an example of getting Form Fields or URL parameters dynamically.


I am giving two files. stage.cfm and stagetest.cfm In stage.cfm it prints fieldnames and data. To test use stagetest.cfm

Code for stage.cfm

<cfoutput>
<cfif isdefined("FORM.FIELDNAMES")>
   <table cellpadding="5" border=1>
   <tr><th colspan="2" align="center">Form Fileds</th></tr>
   <tr><th>Input Field</th><th>Value</th></tr>
   <cfloop index = "fname"  list = "#FORM.FIELDNAMES#"  delimiters = ",">

      <cfset fval = evaluate("FORM.#fname#")>
      <tr><td>#fname#</td><td>#fval#</td></tr>      
       
   </cfloop>
   </table>
</cfif>


<cfif isdefined("CGI.QUERY_STRING") and CGI.QUERY_STRING neq "">
  
   <cfset urlList = CGI.QUERY_STRING>

   <table cellpadding="5" border=1>
   <tr><th colspan="2" align="center">URL Parameters</th></tr>
   <tr><th>URL Parameter</th><th>Value</th></tr>
  
   <cfloop index = "urlParam"  list="#urlList#" delimiters="&">
     
      <cfset parray = ListToArray(urlparam,"=")>
      <cfif ArrayLen(parray) eq 1>
          <cfset parray[2]='null'>
      </cfif>

      <tr><td>#Parray[1]#</td><td>#Parray[2]#</td></tr>      
       
   </cfloop>
   </table>

</cfif>

</cfoutput>


Code for stagetest.cfm

<table cellpadding="5" align="center"><tr><th colspan="2">This is for testing stage.cfm file.</th></tr>
<tr><td>
<form name="frm" method="post" action="stage.cfm?username=xyztest&password=xyztest">
<input type="hidden" name="username" value="xyz">
<input type="hidden" name="password" value="xyz">
<input type="hidden" name="firstname" value="xyz">
<input type="hidden" name="lastname" value="venkat">
<input type="hidden" name="email" value="xyz@xyz.com">
<input type="Submit" name="submit" value="Submit with POST">
</form></td>
<td><form name="frm" method="get" action="stage.cfm">
<form name="frm" method="post" action="stage.cfm">
<input type="hidden" name="username" value="xyz">
<input type="hidden" name="password" value="xyz">
<input type="hidden" name="firstname" value="xyz">
<input type="hidden" name="lastname" value="venkat">
<input type="hidden" name="email" value="xyz@xyz.com">
<input type="hidden" name="dept" value="">
<input type="Submit" name="submit" value="Submit with GET">
</form></td></tr></table>

ilssac
Inspiring
June 12, 2009

Uddaraju Ramesh wrote:

Here is an example of getting Form Fields or URL parameters dynamically.

Here is another example (Without the table formating):

<cfoutput>

<cfloop collection="#form#" item="field">

#field#: #form[field]#<br/>

</cfloop>

<cfloop collection="#url# item="key">

#key#: #url[key]#<br/>

</cfloop>

<cfoutput>

But your code works as well.