Results 1 to 4 of 4
  1. #1
    guinea222 is offline Member
    Join Date
    Sep 2014
    Posts
    10
    Rep Power
    0

    Default Java Total function not working

    I am not sure what I am doing wrong, my teacher gave us this code from a website. However the total on the form does not work at all. I have tried messing with it and came up empty, does anyone have an idea?

    order_form.js

    function checkSubmit(form) {
    if ( form.total.value === '0' ) {
    alert( 'You haven\'t order anything.' );
    return false;
    } else {
    return true;
    }
    }

    function doTotals(form) {
    var total = 0;
    for (var i=0; PRODUCT_ABBRS[i]; i++) {
    var cur_field = form.elements[ PRODUCT_ABBRS[i] + "_qty" ];
    if ( !isPosInt(cur_field.value) ) {
    return;
    }
    total += parseFloat(cur_field.value) * parseFloat( form.elements[ PRODUCT_ABBRS[i] + "_price" ].value );
    }
    form.elements ['total'].value = formatDecimal(total);
    }


    function getProductTotal(field) {
    var form = field.form;

    if (field.value === "") {
    field.value = 0;
    }

    if ( !isPosInt(field.value) ) {
    field.value = 0;
    } else {
    var product = field.name.slice(0, field.name.lastIndexOf("_") );
    var price = form.elements[product + "_price"].value;
    var amt = field.value * price;
    form.elements[product + "_tot"].value = formatDecimal(amt);
    doTotals(form);
    }
    }

    // onclick
    function checkValue(field) {
    if (field.value == 0) {
    field.value = "";
    }
    }

    // onblur
    function reCheckValue(field) {
    if (field.value == "") {
    field.value = 0;
    }
    }

    function isPosInt(val) {
    var re = /^\d+$/;
    if ( !re.test(val) ) {
    return false;
    }
    return true;
    }

    // format val to n number of decimal places
    // modified version of Danny Goodman's (JS Bible)
    function formatDecimal(val, n) {
    n = n || 2;
    var str = "" + Math.round ( parseFloat(val) * Math.pow(10, n) );

    while (str.length <= n) {
    str = "0" + str;
    }
    var pt = str.length - n;
    return str.slice(0,pt) + "." + str.slice(pt);
    }

    Form

    <meta charset="utf-8" />
    <title>Example 2</title>
    <link rel="stylesheet" href="includes/order_form.css" type="text/css" />
    <script src="includes/order_form.js" type="text/javascript"></script>
    <script type="text/javascript">
    var PRODUCT_ABBRS = ["choc_cake","carrot_cake","cheese_cake","banana_br ead"];
    </script>
    </head>
    <body>

    <form action="ex2_result.php" method="post" onsubmit="return checkSubmit(this);">
    <table class="demoTbl">
    <tbody>
    <tr>
    <th class="first">Product</th>
    <th>Price</th>
    <th>Quantity</th>
    <th>Totals</th>
    </tr>
    <tr>
    <td>Radish</td>
    <td class="cur">$3.00 Bunch<input type="hidden" name="choc_cake_price" value="3" /></td>
    <td class="qty"><input type="text" name="choc_cake_qty" value="0" size="4" class="cur" pattern="[0-9]+" placeholder="0" onchange="getProductTotal(this)" onclick="checkValue(this)" onblur="reCheckValue(this)" /></td>
    <td><input type="text" name="choc_cake_tot" value="0" readonly="readonly" size="8" class="cur" /></td>
    </tr>
    <tr>
    <td>Spinach</td>
    <td class="cur">$2.00 Bunch<input type="hidden" name="carrot_cake_price" value="2" /></td>
    <td class="qty"><input type="text" name="carrot_cake_qty" value="0" size="4" class="cur" pattern="[0-9]+" placeholder="0" onchange="getProductTotal(this)" onclick="checkValue(this)" onblur="reCheckValue(this)" /></td>
    <td><input type="text" name="carrot_cake_tot" value="0" readonly="readonly" size="8" class="cur" /></td>
    </tr>
    <tr>
    <td>Turnip</td>
    <td class="cur">$4.00 Bunch<input type="hidden" name="cheese_cake_price" value="4" /></td>
    <td class="qty"><input type="text" name="cheese_cake_qty" value="0" size="4" class="cur" pattern="[0-9]+" placeholder="0" onchange="getProductTotal(this)" onclick="checkValue(this)" onblur="reCheckValue(this)" /></td>
    <td><input type="text" name="cheese_cake_tot" value="0" readonly="readonly" size="8" class="cur" /></td>
    </tr>

    <tr>
    <td class="total" colspan="3">Total: </td>
    <td><input type="text" name="total" value="0" readonly="readonly" size="8" class="cur" /></td>
    </tr>
    <tr>
    <td class="label">First Name: </td>
    <td colspan="3"><input type="text" name="first_name" value="" size="36" /></td>
    </tr>
    <tr>
    <td class="label">Last Name: </td>
    <td colspan="3"><input type="text" name="last_name" value="" size="36" /></td>
    </tr>
    <tr>
    <td class="label">Email: </td>
    <td colspan="3"><input type="text" name="email" value="" size="36" pattern="^[\w\+'\.-]+@[\w'\.-]+\.[a-zA-Z]{2,}$" required="required" /></td>
    </tr>
    <tr>
    <td class="label">Phone: </td>
    <td class="last" colspan="3"><input type="text" name="phone" value="" size="36" /></td>
    </tr>
    <tr>
    <td class="submit" colspan="4"><input type="submit" name="submit" value="Submit" /></td>
    </tr>
    </tbody>
    </table>
    </form>

    </body>
    </html>

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Java Total function not working

    The problem is that this is a Java forum and what you posted is JavaScript with embedded HTML.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Java Total function not working

    Moved from New to Java to Forum Lobby.

    guinea222, any time you have a question about Java code, go through BB Code List - Java Programming Forum - Learn Java Programming before you post.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Java Total function not working

    I find it kind of odd that the PRODUCT_ABBRS array is defined in the main html file but it is only actually used in that external order_form.js file included above it. Maybe it works, HTML and Javascript works in mysterious ways.

    In any case all modern browsers have developer tools built in with a console which will show you whatever javascript error there is. Use that to figure out what is wrong.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. JSP EL function is not working for var arg method.
    By makpandian in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 05-28-2013, 01:12 PM
  2. Replies: 2
    Last Post: 08-20-2012, 06:38 PM
  3. String trim() member function not working
    By shall in forum New To Java
    Replies: 2
    Last Post: 04-29-2012, 07:28 AM
  4. The function append() not working.
    By gautham0209 in forum AWT / Swing
    Replies: 4
    Last Post: 11-20-2011, 07:32 AM
  5. Static function to get current working directory
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 06-23-2008, 02:13 AM

Posting Permissions

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