Skip to main content
Known Participant
July 5, 2006
Question

form data

  • July 5, 2006
  • 18 replies
  • 1461 views
I am using a function to calculate a value based on numbers the user enters in text boxes and checkbox selection. I think the function is working because I get 0 as the value when I process the form. I don't think the values entered by the user are being
stored by the form, though. I don't see any values when I process the form in the browser.

Do I need to do something else to pass the user entries to my function?

Here is relevant code:
<cfscript>

function lcost(DUP,COPIES,ORIG,SINGLE_SIDE)
{Var total = 0;

if ( form.DUP EQ "yes")
total= form.COPIES *(form.ORIG/2)* .047;
return total;
}
</cfscript>
-----------------------------------------------
<cfparam name="FORM.COLL" default="">
<cfparam name="FORM.DRILL" default="">
<cfparam name="FORM.FOLD" default="">
<cfparam name="FORM.BIND" default="">
<cfparam name="FORM.DUP" default="">
<cfparam name="FORM.COPIES" default="">
<cfparam name="FORM.ORIG" default="">

<cfform name="form2" id="form2" method="post" action="submit.cfm">
<label>PAPER
<select name="PAPERID" id="PAPERID" onkeypress="KeyPress()">
<cfoutput query="paperlist">
<option value>="#paperlist.descr# #paperlist.paperid#"</option>
</cfoutput>
</select>



</label>
<label> COPIES
<cfinput type="text" name="COPIES" onfocus="#lcost(form.dup,form.copies,form.orig,paperlist.single_side)#" accesskey="8" tabindex="8" default = "0" size = "10">
DUP
<cfinput type="checkbox" name="DUP" label="DUP" checked="no" accesskey="10" tabindex="10" value= "">
</label>
<label>DRILL
<cfinput type="checkbox" name="DRILL" value="DRILL" accesskey="11" tabindex="11">
</label>
<label>FOLD
<cfinput type="checkbox" name="FOLD" value="FOLD" accesskey="12" tabindex="12">
</label>
<label>BIND
<cfinput type="checkbox" name="BIND" value="BIND" accesskey="13" tabindex="13">
</label>
<label>COLL/STAPLE
<cfinput type="checkbox" name="COLL" value="COLL" accesskey="13" tabindex="13">
</label>
<label>AMT
<cfoutput>
<input type="text" name="amt" value="#lcost(form.dup,form.copies,form.orig,.047)#" >
</cfoutput>
</label>

<p>
<cfinput type="text" name="ORIG" size = "10" >

</p>
</cfform>
This topic has been closed for replies.

18 replies

Inspiring
July 6, 2006
You pass it back to to ColdFusion in the form. If you are updating the
amt input field with your JavaScript that you originally where trying to
call the ColdFusion function from, when the form is submitted,
#Form.amt# will contain the calculated value from the client.

Deb3 wrote:
> I decided to use javascript because I think you were both steering me in that
> direction. (The form should use the entries to calculate while the user is
> entering them) I did make the function reuseable, as suggested. However,
> coldfusion does not recognize the return variable #total#. I have tried to
> find information on how to pass a Java script variable back to Coldfusion
> without success. Is this possible? If so, how?
>
Deb3Author
Known Participant
July 6, 2006
I decided to use javascript because I think you were both steering me in that direction. (The form should use the entries to calculate while the user is entering them) I did make the function reuseable, as suggested. However,
coldfusion does not recognize the return variable #total#. I have tried to find information on how to pass a Java script variable back to Coldfusion without success. Is this possible? If so, how?
Inspiring
July 6, 2006
A more basic problem with this is that the OP is trying to write a
JavaScript function with ColdFusion syntax.


BKBK wrote:
> As I suggested to you
> http://www.adobe.com/cfusion/webforums/forum/messageview.cfm?catid=2&threadid=11
> 69351, a function should be reusable. Note that, in the following example,
> var1, var2, var3 and var4 are dummy variables. They stand for any set of 4
> variables that you pass to the function.
>
> <cfscript>
> function lcost(var1, var2, var3, var4)
> {
> var total = 0;
> if ( var1 EQ "yes") total= var2 *(var3/2)* var4;
> return total;
> }
> </cfscript>
>
> Thus, the function doesn't know form.DUP. It will know it only if you pass
> form.DUP as one of the arguments when you call the function. Suppose that
> form.DUP is 5, form.copies is 7, form.orig is 6 and paperlist.single_side is
> .047. Then the code
>
> <cfset myTotal = lcost(form.dup,form.copies,form.orig,paperlist.single_side)>
> <cfoutput>#myTotal#</cfoutput>
>
> will display 0.987.
>
> The function checks whether form.dup is "yes", which is true because 5 is
> "yes" in Coldfusion. Then it perfoems the calculation total = 6*(7/2)*0.047 and
> returns the result, namely, 0.987.
>
>
>
>
>
>
BKBK
Community Expert
Community Expert
July 6, 2006
As I suggested to you elsewhere in the forum, a function should be reusable. Note that, in the following example, var1, var2, var3 and var4 are dummy variables. They stand for any set of 4 variables that you pass to the function.

<cfscript>
function lcost(var1, var2, var3, var4)
{
var total = 0;
if ( var1 EQ "yes") total= var2 *(var3/2)* var4;
return total;
}
</cfscript>

Thus, the function doesn't know form.DUP. It will know it only if you pass form.DUP as one of the arguments when you call the function. Suppose that form.DUP is 5, form.copies is 7, form.orig is 6 and paperlist.single_side is .047. Then the code

<cfset myTotal = lcost(form.dup,form.copies,form.orig,paperlist.single_side)>
<cfoutput>#myTotal#</cfoutput>

will display 0.987.

The function checks whether form.dup is "yes", which is true because 5 is "yes" in Coldfusion. Then it performs the calculation total = 6*(7/2)*0.047 and returns the result, namely, 0.987.





Inspiring
July 6, 2006
Nope, cffunction and cfscript and anything else that starts with "cf" is
ColdFusion and runs on the server. You want to put it in plain <script>
tags, write the code with JavaScript (not ColdFusion) syntax and most
likely put it into the <head></head> section of your page.

Deb3 wrote:
> I appreciate finding out that I need to distinguish commands specific to the
> client and server. I'm unfamiliar with this. How should I get my form to work?
> Does the cfscript need to be a cffunction? Is there a event that runs on the
> server?
>
Deb3Author
Known Participant
July 6, 2006
I appreciate finding out that I need to distinguish commands specific to the client and server. I'm unfamiliar with this. How should I get my form to work?
Does the cfscript need to be a cffunction? Is there a event that runs on the server?
Inspiring
July 6, 2006
Your function is inside cfscript tags, which means it runs on the server. You call it with an onFocus event, which happens on the client. That just ain't gonna work.
Inspiring
July 5, 2006
A couple of suggestions. <cfdump var="#form#"> at the top of your
action page will let you know what values you are getting for your form
fields assuming you are on a relatively recent version of CF.

Secondly in you function, you are passing in the values so instead of
using form.Dup ect., you should use arguments.dup ect inside the
function. These will reference the values defined in your function line
and not attempt to access the values from outside the function.

Where you have called the function, the values are going to be empty
strings until the form is submitted. If the form is not self submitting
,this code is on submit.cfm defined in the form action parameter, then
these values will never be set on this template.

If you are trying to have the value of the amt field change as users
complete the form and provide their desired values in the relative
fields, you are looking for a JavaScript solution that will run on the
client, not a ColdFusion solution that can only work on the server.

Ian Skinner

Deb3 wrote:
> I am using a function to calculate a value based on numbers the user enters in
> text boxes and checkbox selection. I think the function is working because I
> get 0 as the value when I process the form. I don't think the values entered by
> the user are being
> stored by the form, though. I don't see any values when I process the form in
> the browser.
>
> Do I need to do something else to pass the user entries to my function?
>
> Here is relevant code:
> <cfscript>
>
> function lcost(DUP,COPIES,ORIG,SINGLE_SIDE)
> {Var total = 0;
>
> if ( form.DUP EQ "yes")
> total= form.COPIES *(form.ORIG/2)* .047;
> return total;
> }
> </cfscript>
> -----------------------------------------------
> <cfparam name="FORM.COLL" default="">
> <cfparam name="FORM.DRILL" default="">
> <cfparam name="FORM.FOLD" default="">
> <cfparam name="FORM.BIND" default="">
> <cfparam name="FORM.DUP" default="">
> <cfparam name="FORM.COPIES" default="">
> <cfparam name="FORM.ORIG" default="">
>
> <cfform name="form2" id="form2" method="post" action="submit.cfm">
> <label>PAPER
> <select name="PAPERID" id="PAPERID" onkeypress="KeyPress()">
> <cfoutput query="paperlist">
> <option value>="#paperlist.descr# #paperlist.paperid#"</option>
> </cfoutput>
> </select>
>
>
>
> </label>
> <label> COPIES
> <cfinput type="text" name="COPIES"
> onfocus="#lcost(form.dup,form.copies,form.orig,paperlist.single_side)#"
> accesskey="8" tabindex="8" default = "0" size = "10">
> DUP
> <cfinput type="checkbox" name="DUP" label="DUP" checked="no" accesskey="10"
> tabindex="10" value= "">
> </label>
> <label>DRILL
> <cfinput type="checkbox" name="DRILL" value="DRILL" accesskey="11"
> tabindex="11">
> </label>
> <label>FOLD
> <cfinput type="checkbox" name="FOLD" value="FOLD" accesskey="12"
> tabindex="12">
> </label>
> <label>BIND
> <cfinput type="checkbox" name="BIND" value="BIND" accesskey="13"
> tabindex="13">
> </label>
> <label>COLL/STAPLE
> <cfinput type="checkbox" name="COLL" value="COLL" accesskey="13"
> tabindex="13">
> </label>
> <label>AMT
> <cfoutput>
> <input type="text" name="amt"
> value="#lcost(form.dup,form.copies,form.orig,.047)#" >
> </cfoutput>
> </label>
>
> <p>
> <cfinput type="text" name="ORIG" size = "10" >
>
> </p>
> </cfform>
>
>