Ask a Question related to Macromedia ColdFusion, Design and Development.
-
Cindy587 #1
Calculate values
Hello
I have a form with 26 fields and am trying to use a function for calculating a
particular quantity.
The user enters two values (VendorPrice and QuantityPerUnit) and selects the
Update button. The following logic makes perfect sense to me but apparently
ColdFusion is not impressed. When selecting the Update button, the entire form
resets to the default values. Not quite what I was trying to achieve.
I also need to calculate 3 additional values based on the vendor price and
margins (markup percentages). Hopefully once I understand the correct process,
adding the remaining calculations won?t be a problem.
<cffunction name="updateQuantity" returntype="numeric" output="false">
<cfargument name="QuantityPerUnit" required="true" type="numeric">
<cfargument name="VendorPrice" required="true" type="numeric">
<cfset UnitPrice =0>
<cfset UnitPrice = VendorPrice / QuantityPerUnit>
<cfreturn UnitPrice>
</cffunction>
<cfscript>
if( isDefined("form.update_qty") )
{
if ((Form.VendorPrice GT 0) AND (Form.QuantityPerUnit GT 0))
{
updateQuantity(QuantityPerUnit, VendorPrice);
}
}
</cfscript>
<cfform>
26 input fields
<input type="submit" name=?create? value="Create record">
<input type="submit" name="update_qty" value="Update values">
<input type="submit" name="cancel" value="Cancel">
</cfform>
Any suggestions would be appreciated.
Thanks
Cindy
Cindy587 Guest
-
Calculate Cumulative
Hi, I have a database where I have to find the summation of points for each person. The example below has the name repeating multiple times. I am... -
calculate using in-query values
I'm attempting to perform a calculation at the query level and have run into the following problem. I would like to calculate a percentage using... -
calculate a String
I have a simple String-Problem: A textfield-variable called "display" contains a string like "(12/2)*3". when i click on a button, the content of... -
Calculate Age from DOB
I need a calculation that indicates how old (based on the DOB field) someone is in years /months/days. I'd be grateful for years and months only if... -
Calculate next available IP
Is there a module, or a readily available algorithm, for calculating the next available IP Address? Let's say I have an array... -
anclarke #2
Re: Calculate values
What do you mean about the form resetting to its default values? Is this
because you have something like <cfinput type='text' name='quantityPerUnit'
value='' />? Sorry if this is more basic than what you're asking, but you know
you have to set the value in the form input, right? Otherwise the form will
just have whatever 'default value' it had before. If what you're saying is
that the form object doesn't contain the values that the user entered on the
previous page, well, are you sure about that? Did you look at the form info in
the debugging output? Or possibly try <cfdump var='#FORM#' /> Maybe you're
accidentally using GET instead of POST so all your variables are in the URL
scope instead. Your <cfform> comment above of course does not provide any
insight here. Maybe you also have a cflocation somewhere between when the user
hits 'Update values' and when your code above runs. Good luck, - Andrew.
anclarke Guest
-
Cindy587 #3
Re: Calculate values
Thank you Andrew.
At this point, my knowledge of ColdFusion is very basic since I only began
working with it a couple of weeks ago.
The issue with the form resetting was a typo which I didn?t notice until
looking at the code again after your response.
However, the value I am trying to calculate still is not returning. The
following has the detail you mentioned was missing from my original post. I
copied the exact form code, omitting 23 non-relevant fields?but nothing was
omitted other than <tr>, <td> and <cfinput> (no cflocation or other tags).
Thanks for your help
Cindy
<cffunction name="updateQuantity" returntype="numeric" output="false">
<cfargument name="QuantityPerUnit" required="true" type="numeric">
<cfargument name="VendorPrice" required="true" type="numeric">
<cfset UnitPrice =0>
<cfset UnitPrice = VendorPrice / QuantityPerUnit>
<cfreturn UnitPrice>
</cffunction>
<cfscript>
if( isDefined("form.update_qty") )
{
if ((Form.VendorPrice GT 0) AND (Form.QuantityPerUnit GT 0))
{
updateQuantity(QuantityPerUnit, VendorPrice);
}
}
</cfscript>
<cfform method="post" preservedata="yes">
<table width="600" border="0" cellpadding="0" cellspacing="0" class="formtext">
<tr valign="baseline">
<td nowrap align="right">Primary Vendor ID: </td>
<td>
<cfselect name="VendorID" required="Yes" message="Please select a
Vendor.">
<cfoutput query="GetVend">
<option value="#GetVend.VendorID#">
#GetVend.CompanyName#
</option>
</cfoutput>
</cfselect>
<td nowrap align="right">Vendor Cost: </td>
<td colspan="3">
<cfinput type="text"
name="VendorPrice"
value="0"
size="15"
validate="integer"
validateAt="onSubmit,onServer"
required="yes"
message="Vendor Price requires numeric entry."
class="formtext">
</td>
</tr>
<tr valign="baseline">
<td colspan="6"> </td>
</tr>
<tr valign="baseline">
<td nowrap align="right">Quantity Per Unit: </td>
<td>
<cfinput type="text"
name="QuantityPerUnit"
value="0"
size="15"
validate="integer"
validateAt="onSubmit,onServer"
required="yes"
message="Quantity Per Unit requires numeric entry."
class="formtext">
</td>
<td nowrap align="right">Unit Cost: </td>
<td colspan="3">
<cfinput name="UnitPrice" value=0 size="15" class="formtext">
</td>
</tr>
<input type="submit" name=?create? value="Create record"
onmouseover="this.className='createbuttonon'"
onmouseout="this.className='createbutton'" class="createbutton">
<input type="submit" name="update_qty" value="Update values"
onmouseover="this.className='createbuttonon'"
onmouseout="this.className='createbutton'" class="createbutton">
<input type="submit" name="cancel" value="Cancel"
onmouseover="this.className='createbuttonon'"
onmouseout="this.className='createbutton'" class="createbutton">
</table>
</cfform>
Cindy587 Guest
-
anclarke #4
Re: Calculate values
Instead of: updateQuantity(QuantityPerUnit, VendorPrice); try:
form.unitPrice = updateQuantity(QuantityPerUnit, VendorPrice); when you use
preserveData='yes' in your cfform definition, you're telling ColdFusion to
automatically populate the inputs with data from the form scope if it's
available. Therefore, you have to populate the form scope with the data you
want to display if this is how you are going to do it. Note that I'm just
trying to answer your question, not rewrite your code to how I would do it. To
each their own! :-) - Andrew.
anclarke Guest
-
Cindy587 #5
Re: Calculate values
I have spent the entire weekend messing with this and am no further
ahead?simply cannot get the form values to pass to the function. What I want
to achieve is the following:
1. A form for creating a new inventory item with 4 button options (Create,
Calculate Value, Reset Form and Cancel). Within the form have 4 values that
are calculated (but not written to the database) based on other field?s input
values. Example: UnitCost is the result of calculating VendorPrice (input) and
QuantityPerUnit (input).
2. A way of determining which button was clicked (Create, Calculate Values,
Reset Form and Cancel) and initiating an associated defined activity.
3. A method for calculating values based on form input and returning that
value to the form. (When the user selects the Calculate Value button, the form
is updated with the calculated values)
4. A method for creating a new record in the database.
Most likely my code is pathetic since I have just began working with
ColdFusion. If you have any suggestions on how to get where I need to be,
PLEASE feel free to comment on anything I have written.
Thanks
Cindy
Cindy587 Guest
-
-
anclarke #7
Re: Calculate values
Originally posted by: SafariTECH Just as a footnote, CF always reloads a page
when you process something - it is a server side language, not client side,
therefore if your fields have pre-set values, that is what it will use. i.e.
<cfinput type='text' name='QuantityPerUnit' value='0' size='15'
validate='integer' validateAt='onSubmit,onServer' required='yes'
message='Quantity Per Unit requires numeric entry.' class='formtext While this
is generally true, in this case Cindy is right in what she's doing. She's
using the preservedata='yes' in her cfform definition, which means that
ColdFusion will populate her form inputs with values from the form variable
scope if they exist. Check out the docs for cfform if you wonder what I mean.
Cindy, the code you have should satisfy your requirements 1-3 listed above.
You still need to add a cfquery or cfstoredproc tag (you could also use
cfinsert & cfupdate although I personally never do) to put the data in the
database. Some of the other suggestions I have revolve around your use of a
cffunction here. You're only calling it from one place, and if so I don't know
there's any use to making a function, really. Your code would operate a little
more efficiently if you just did: if ((Form.VendorPrice GT 0) AND
(Form.QuantityPerUnit GT 0)) form.unitPrice = vendorPrice / quantityPerUnit;
In cases where you use a cffunction, I'd *personally* in a case like this
locally scope my unitPrice variable in the cffunction, i.e. <cfset var
unitPrice = vendorPrice / quantityPerUnit /> The 'var' means that the variable
is only used within the function. Then since you're returning the value,
that's probably a better way of accessing it rather than setting the global
unitPrice variable, if that makes sense. Also, for that matter, you could have
just simplified further with <cfreturn evaluate(vendorPrice / quantityPerUnit)
/> ;-) Have fun, - Andrew.
anclarke Guest
-
Cindy587 #8
Re: Calculate values
Thank you Andrew. I really appreciate your input.
Your suggestion to ?streamline? the code by removing the cffunction and to
replace its functionality within the cfscript worked perfectly. Yeah!
I left the if( isDefined("form.update_qty") ) in the script and added another
4 if statements to check for input values and perform the 4 independent
calculations. Also added 4 cfset to establish 0 value for each variable that
will be calculated. (Eliminated an error).
Thanks again
Cindy
Cindy587 Guest



Reply With Quote

