Skip to main content
Inspiring
August 15, 2019
Answered

Dynamic Form Values -Outputting

  • August 15, 2019
  • 1 reply
  • 1239 views

I have a form that submits to itself like this

<form action="" method="post">

<table >

   <tr>

  <td width="50"><input type="checkbox" name="client_id_C" value="#client_id#"   ></td>

   <td  width="50"><input type="text"  name="#client_id#_Fee"  value="">  </td>

</tr>

</table>

</form>

The client_IDs are 101,102,103 etc

So when I fill out the form I get form values like this

101_Fee = 1000

102_Fee = 2000

103_Fee = 3000

I also get a list of the client_id's like

client_id_C = 20864,21238,21284

My question is once the form is submitted and the form reloads.

I want the fee values to populate the #client_id#_Fee text boxes, like this

<form action="" method="post">

<table >

   <tr>

  <td width="50"><input type="checkbox" name="client_id_C" value="#client_id#"   ></td>

   <td  width="50"><input type="text"  name="#client_id#_Fee"  value="#(#client_id#_Fee)#">  </td>

</tr>

</table>

</form>

Obviously what I have in red doesn't work

The problem is how do I code to get the value?

For example

I want the value for client_ID 101 to be 1000

Like this

<form action="" method="post">

<table >

   <tr>

  <td width="50"><input type="checkbox" name="client_id_C" value="101"   ></td>

   <td  width="50"><input type="text"  name="101_Fee"  value="1000">  </td>

</tr>

   <tr>

  <td width="50"><input type="checkbox" name="client_id_C" value="102"   ></td>

   <td  width="50"><input type="text"  name="102_Fee"  value="2000">  </td>

</tr>

</table>

</form>

What code goes in the value where the red text is.?

Because the variable already contains # tags.

This topic has been closed for replies.
Correct answer Charlie Arehart

Try:

<input type="text"  name="#client_id#_Fee"  value="#form["#client_id#"_Fee]#">

As a proof (that we are understanding what you mean), this produces the 1000 you seek:

<cfset form.101_Fee = 1000>

<cfset form.client_id=101>

<cfoutput>#form["#client_id#_fee"]#</cfoutput>

That said, when things get so convoluted, it's usually time to start reconsidering the design choices made for those form fields. But until then, this use of array notation to refer to a variable name built dynamically is a common solution for such challenges.

Let us know if that works for you.

1 reply

Charlie Arehart
Community Expert
Charlie ArehartCommunity ExpertCorrect answer
Community Expert
August 16, 2019

Try:

<input type="text"  name="#client_id#_Fee"  value="#form["#client_id#"_Fee]#">

As a proof (that we are understanding what you mean), this produces the 1000 you seek:

<cfset form.101_Fee = 1000>

<cfset form.client_id=101>

<cfoutput>#form["#client_id#_fee"]#</cfoutput>

That said, when things get so convoluted, it's usually time to start reconsidering the design choices made for those form fields. But until then, this use of array notation to refer to a variable name built dynamically is a common solution for such challenges.

Let us know if that works for you.

/Charlie (troubleshooter, carehart. org)
weezerboyAuthor
Inspiring
August 16, 2019

The master has spoken. Thank you, Charlie!

Charlie Arehart
Community Expert
Community Expert
August 16, 2019

Well, I wouldn't put it that way, but thanks if that was indeed meant as a compliment. :-) And glad I could help.

/Charlie (troubleshooter, carehart. org)