/    Sign up×
Community /Pin to ProfileBookmark

calculations

I am new at JavaScript. I have created an asp that our customers will use to obtain an estimate for a service we perform. Data they enter into the form will be submitted to us through email – that part works fine.

The form contains several fields where the user will indicate the number of pieces of equipment that they have requiring service. When the user clicks a button, I need the form to calculate the following: Estimated Cost = (NumberEquipment1 * CostPerEquipment1) + (NumberEquipment2 * CostPerEquipment2), etc……for each equipment type. Once calculated, the result should display on the form.

I have no clue as to where to begin. To this point, all I have created are forms requiring customer input, but no calculations. Any help would be greatly appreicated.

CS

to post a comment
JavaScript

12 Comments(s)

Copy linkTweet thisAlerts:
@Sux0rZh_jc0rzAug 28.2003 — I'm no guru at javascript calculations so I'll send you here:

http://www.w3schools.com/js/default.asp

If you know a little about javascript you can skip some of that and go directly here:

http://www.w3schools.com/js/js_math.asp

I'll also be working on a code for you cause I'd like to know how to do this too. But don't expect me to have the code within the next half hour ?
Copy linkTweet thisAlerts:
@CShellauthorAug 28.2003 — Thank you ever-so. I'll check out the sites and let you know!
Copy linkTweet thisAlerts:
@Sux0rZh_jc0rzAug 28.2003 — [code=php]
<html>
<head>

<script type="text/javascript">
function times(numberA,numberB)
{
return numberA * numberB
}
</script>

</head>
<body>

<script type="text/javascript">
document.write(times(2,3))
</script>

<p>The script in the body section calls a function with two arguments, 2 and 3.</p>

<p>The function returns the sum of these two arguments.</p>

</body>
</html>
[/code]


this is what i'm tinkering with atm. trying to assign the input boxes in there...
Copy linkTweet thisAlerts:
@Sux0rZh_jc0rzAug 28.2003 — ima look at a simple calculator script and see if i can't pull it off there..
Copy linkTweet thisAlerts:
@David_HarrisonAug 28.2003 — Could you modify this to get what you want?

[upl-file uuid=db91a1c0-7c41-4143-a036-2a01a92307b2 size=1kB]cost calculator.txt[/upl-file]
Copy linkTweet thisAlerts:
@CShellauthorAug 29.2003 — Thank you both for your suggestions. I will try manipulating the different codes to see what I can come up with. It may be worth mentioning that the CostPerEquipment is a Constant - one that the customer would neither see, nor input. I was able to write a function in VBScript, but it didn't play well with the rest of the code. I had to define the variable Constants, but I don't know how to do that in JavaScript.

If you have an epiphany, let me know! ?
Copy linkTweet thisAlerts:
@CShellauthorAug 29.2003 — I tried working with the suggestions posted last evening with no success. I will attempt to make what I am looking for more clear.

The code that creates the field into which the customer inputs a number is this:

<TR>

<TD>Condensers:</TD>

<TD colSpan="3"><input type="text" size=2 name="Condensers" >

<input type="hidden" name="15f" value="Condensers">

</td>

The value 15f is passed in an email to me when the customer clicks the submit button. We are gathering information on 6 other types of equipment. All are coded in the same manner.

What I need to understand how to do is this:

  • 1. The CostPerEquipment is a contant. Say...$10 to service each compressor. How do I define these constants. var CostPerCompressor = 10 ? (Or do I need to define them?)


  • 2. I need to know the correct syntax to write a function that would calculate the product of NumberOfCompressors*CostPerCompressor, etc...


  • 3. How do I add the ProductForCompressors to the Products for the other equipment types?


  • 4. Finally, I would like the user to click a button to run the function and display the answer in a field called Estimated Cost.


  • I hope this is more clear. Thank you again for your time and any help that you may be able to provide.

    Christine
    Copy linkTweet thisAlerts:
    @ssurmiAug 29.2003 — If there are a number of "costPerEquipment", you can just define them in an array at the top and use them as needed. For example:

    <script language="JavaScript" type="text/javascript">

    aCostPerEquip = new Array("sEquip1" , 5,

    "sEquip2", 7);

    function CalculateCost(oForm) {

    for (var n = 0; n < aCostPerEquip.length; n += 2) {

    var sVarName = aCostPerEquip[n];
    var oInput = eval("oForm." + sVarName);

    //you could also do: var oInput = document.thsiForm.sVarname

    if (sVarName == "sEquip1") {

    var iNumEquip1 = parseInt(oInput.value);


    var iTotalCost = iNumEquip1*aCostPerEquip[n+1); }

    }

    }

    </script>


    //Now below you will define the form and call to the function above.


    <form method="post" action="" name="thisForm" id="thisForm" onSubmit="">

    <tr>

    <td colspan=3 class="bodycopy">Equipment1: <br>

    <input name="sEquip1" size=40 maxlength=35>

    </td>

    </tr>

    <tr>

    <td colspan=3 class="bodycopy">Equipment2:<br>

    <input name="sEquip2" size=40 maxlength=35>
    </td>
    </tr>

    ......
    Copy linkTweet thisAlerts:
    @CShellauthorAug 29.2003 — Good morning, ssurmi!

    Thank you for your help. :-) Please forgive me for my ignorance, however. Being new to all of this, I am at a loss as to how the script you posted works. I need to know what "names" in your code to replace with "names" in mine. For example:

    aCostPerEquip = new Array("sEquip1" , 5, "sEquip2", 7);

    Is "sEquip1" = Compressors / "sEquip2" = Condensers, etc?

    Are the "5" and "7" equal to the Cost of service for each? So that if Compressors cost $10 and Condensers $20, my code would appear as:

    aCostPerEquip = new Array("Compressors" , 10, "Condensers", 20);


    Or, am I way off track. I really am sorry that I don't understand.

    Again...thank you for your time and assistance.
    Copy linkTweet thisAlerts:
    @ssurmiAug 29.2003 — Yes those needs to be defined like as you mentioned. Also add a "Button" and call the function to it's onClick event. Any javascript book can show you how to do that.

    Hope this helps.
    Copy linkTweet thisAlerts:
    @CShellauthorAug 29.2003 — Thank you... I have created the array and I know how to call the function to a button.

    Now, in this portion of your code, what do I need to change to suit my needs:

    for (var n = 0; n < aCostPerEquip.length; n += 2) {

    var sVarName = aCostPerEquip[n];

    var oInput = eval("oForm." + sVarName);

    I don't understand what the above code does. I pasted it as-is into my code and the following error message results:

    'oForm.MachRooms' is null or not an object. It must not be an object, as I typed a value of "5" into the field.

    Too...how do I get the result of the function to display on the form? document.write....?

    Thanks again for your assistance!
    Copy linkTweet thisAlerts:
    @CShellauthorAug 29.2003 — I'm just wondering if I'm getting any closer. This is a small portion of what the form looks like, now. I get the error mentioned in my last post. What is it that I am doing wrong? Please don't roll your eyes at me.....Thanks again......

    <SCRIPT LANGUAGE = "JavaScript" type="text/javascript">

    aCostPerEquip = new Array("MachRooms", 10,"Compressors", 20);

    function GetEstimate(oForm) {

    for (var n = 0; n < aCostPerEquip.length; n += 2) {

    var oInput = document.MICostEst.MachRooms

    var oInput = document.MICostEst.Compressors

    if (sVarName == "MachRooms")

    if (sVarName == "Compressors")

    var iNumMachRooms = parseInt(oInput.value);

    var iNumCompressors = parseInt(oInput.value);

    var iTotalCost = iNumMachRooms*aCostPerEquip[n+1];

    var iTotalCost = iNumCompressors*
    aCostPerEquip[n+1];


    }

    function Validate()
    {
    {
    f = document.form1

    }

    f.submit();

    }


    </script>

    </HEAD>

    <BODY>

    <TABLE cellSpacing=0 cellPadding=2 width="100%" border=1>

    <TR>

    <form action="http://www.OurCompany.com/email.asp" method="post" onSubmit="Validate();" id="form1" name="form1">

    <input type="hidden" name="fcount" value="2"><input type="hidden" name="thanksurl" value="http://www.OurCompany/MI/thankyou.html"><input type="hidden" name="recipient" value="[email protected]">

    <TR>


    <td colspan="4"><b>Please enter the number of:</b></td>

    </tr>

    <TR>

    <td>Machine Rooms:</td>

    <td colspan="3"><input type="text" name="MachRooms" size="2" >

    <input type="hidden" name="1f" value="MachRooms"></td>

    </TD>

    <TR>

    <TD>Compressors:</TD>

    <TD colspan="3"><input type="text" name="Compressors" size="2">

    <input type="hidden" name="2f" value="Compressors">

    </td>

    </TR>

    <TR>

    <TD><B>Estimated Cost:</B></td>

    <td>How do I get the result to display here></TD>

    </TD>

    <TR>

    <TD><input type="Button" onclick="GetEstimate();" value="Calculate Estimate" >

    <td><input type="Submit" name = "Submit" value="Request Written Proposal" >

    </td>

    </tr>

    </table>

    </form>

    </BODY>

    </HTML>:rolleyes: :rolleyes:
    ×

    Success!

    Help @CShell spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.17,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @nearjob,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,
    )...