SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Sep 2004
    Location
    seattle
    Posts
    306
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    javascript calculation

    i have this js that does calculation on my php form:

    <SCRIPT>

    function dp(cost)
    {
    string = "" + cost;
    number = string.length - string.indexOf('.');
    if (string.indexOf('.') == -1)
    return string + '.00';
    if (number == 1)
    return string + '00';
    if (number == 2)
    return string + '0';
    if (number > 3)
    return string.substring(0,string.length-number+3);
    return string;
    }


    function calculate()
    {
    document.calcform.sub1.value = dp((document.calcform.quan1.value)*(document.calcform.cost1.value))
    document.calcform.sub2.value = dp((document.calcform.quan2.value)*(document.calcform.cost2.value))
    document.calcform.sub3.value = dp((document.calcform.quan3.value)*(document.calcform.cost3.value))
    document.calcform.sub4.value = dp((document.calcform.quan4.value)*(document.calcform.cost4.value))
    document.calcform.sub5.value = dp((document.calcform.quan5.value)*(document.calcform.cost5.value))
    document.calcform.sub6.value = dp((document.calcform.quan6.value)*(document.calcform.cost6.value))
    document.calcform.sub7.value = dp((document.calcform.quan7.value)*(document.calcform.cost7.value))
    document.calcform.sub8.value = dp((document.calcform.quan8.value)*(document.calcform.cost8.value))
    document.calcform.sub9.value = dp((document.calcform.quan9.value)*(document.calcform.cost9.value))
    document.calcform.sub10.value = dp((document.calcform.quan10.value)*(document.calcform.cost10.value))
    document.calcform.sub11.value = dp((document.calcform.quan11.value)*(document.calcform.cost11.value))
    document.calcform.sub12.value = dp((document.calcform.quan12.value)*(document.calcform.cost12.value))
    document.calcform.sub13.value = dp((document.calcform.quan13.value)*(document.calcform.cost13.value))
    document.calcform.sub14.value = dp((document.calcform.quan14.value)*(document.calcform.cost14.value))
    document.calcform.sub15.value = dp((document.calcform.quan15.value)*(document.calcform.cost15.value))
    document.calcform.sub16.value = dp((document.calcform.quan16.value)*(document.calcform.cost16.value))
    document.calcform.hours1.value = dp((document.calcform.hours1.value)*26)
    document.calcform.hours2.value = dp((document.calcform.hours2.value)*26)
    document.calcform.ltotal.value = dp((document.calcform.hours1.value)+(document.calcform.hours2.value))
    document.calcform.trash.value = dp((document.calcform.trash.value))
    document.calcform.grand.value = dp(eval(document.calcform.sub1.value) + eval(document.calcform.sub2.value) + eval(document.calcform.sub3.value) + eval(document.calcform.sub3.value) + eval(document.calcform.sub4.value) + eval(document.calcform.sub5.value) + eval(document.calcform.sub6.value) + eval(document.calcform.sub7.value) + eval(document.calcform.sub8.value) + eval(document.calcform.sub9.value) + eval(document.calcform.sub10.value) + eval(document.calcform.sub11.value) + eval(document.calcform.sub12.value) + eval(document.calcform.sub13.value) + eval(document.calcform.sub14.value) + eval(document.calcform.sub15.value) + eval(document.calcform.sub16.value) + eval(document.calcform.ltotal.value) + eval(document.calcform.trash.value))

    }

    </SCRIPT>

    for some reason when i enter values for hours1 and hours 2 it's not calculating correctly. then when i enter a value for trash it adds to the hours 1 and hours 2.

    i would like for it to entered of hours1 * 26 then
    hours2 * 26 then give me the total in the labor field.
    then the labor field and trash field should be added to the grandtotal field.

  2. #2
    SitePoint Addict ALL's Avatar
    Join Date
    Oct 2005
    Location
    South Dakota
    Posts
    215
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    first thing... you should never use items as variables that have special meanings like: string = "" + cost;

    that "could" cause problems because "String" is also a function/type.

    second... youd you please post all the source including HTML? it is really hard to help with a problem when you only have half the info,

    -ALL
    Did I help you?
    You can repay me, support one of my projects (no money needed):
    JavaScript Wiki, Another Web Forum, Paranormal Site

  3. #3
    SitePoint Addict
    Join Date
    Sep 2004
    Location
    seattle
    Posts
    306
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    here's my code to display the form
    ?php
    $sQuery = mysql_query("SELECT sid, product, cost FROM supply_code");
    while ($sRow = mysql_fetch_row($sQuery))
    {
    print ("<tr><td bgcolor='#CCD9F0'><input type='hidden' name='prod$sRow[0]' value='$sRow[0]'>$sRow[1]</td>");
    print ("<td bgcolor='#CCD9F0'><input type='text' name='quan$sRow[0]' ONCHANGE='calculate()'></></td>");
    print ("<td bgcolor='#CCD9F0'><input type='hidden' name='cost$sRow[0]' value='$sRow[2]' ONCHANGE='calculate()'>$sRow[2]</td>");
    print ("<td bgcolor='#CCD9F0'><input type='text' name='sub$sRow[0]' value='' ONCHANGE='calculate()'></td>");

    print ("</tr>");
    }
    ?>
    <tr>
    <td width="10&#37;" align="right" bgcolor="#CCD9F0"><div class="label">Hours 1</div></td>
    <td bgcolor="#CCD9F0"><input type=text name="hours1" size="10" ONCHANGE="calculate()" /></td>
    </tr>
    <tr>
    <td width="10%" align="right" bgcolor="#CCD9F0"><div class="label">Hours 2</div></td>
    <td bgcolor="#CCD9F0"><input type=text name="hours2" size="10" /></td>
    </tr>
    <tr>
    <td width="10%" align="right" bgcolor="#CCD9F0"><div class="label">Labor Total</div></td>
    <td bgcolor="#CCD9F0"><input type=text name="ltotal" size="10" ONCHANGE="calculate()" /></td>
    </tr>
    <tr>
    <td width="10%" align="right" bgcolor="#CCD9F0"><div class="label">Trash Removed</div></td>
    <td bgcolor="#CCD9F0"><input type=text name="trash" size="10" ONCHANGE="calculate()" /></td>
    </tr>
    <tr>
    <td width="10%" align="right" bgcolor="#CCD9F0"><div class="label">Grand Total</div></td>
    <td bgcolor="#CCD9F0"><input type=text name="grand" size="10" ONCHANGE="calculate()" /></td>
    </tr>

  4. #4
    SitePoint Addict ALL's Avatar
    Join Date
    Oct 2005
    Location
    South Dakota
    Posts
    215
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    try playing with parseInt(document.calcform.whatever.value) or parseFloat()

    before you pass it though. This will cause it to convert into a number rather than a string... because in javascript + means (add) like in math and (join) like "put these two together."

    This should fix your problem,
    -ALL
    Did I help you?
    You can repay me, support one of my projects (no money needed):
    JavaScript Wiki, Another Web Forum, Paranormal Site

  5. #5
    &#083;itePoint Aficionado JVLB's Avatar
    Join Date
    Jan 2002
    Location
    N 44 56.537' W 123 3.683'
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should be able to simplify your script and eliminate those highly inefficient eval statements if you went to associative array syntax to address your form elements.

    Example:
    document.formOne.named_element can be written as document["formOne"]["named_element"]

    Since the references are strings, string variables can be substituted, allowing loops to do the work rather than having long lists of form elements.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •