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

    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?


    function checkSubmit(form) {
    if ( === '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) ) {
    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 =,"_") );
    var price = form.elements[product + "_price"].value;
    var amt = field.value * price;
    form.elements[product + "_tot"].value = formatDecimal(amt);

    // 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);


    <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"];

    <form action="ex2_result.php" method="post" onsubmit="return checkSubmit(this);">
    <table class="demoTbl">
    <th class="first">Product</th>
    <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>
    <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>
    <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>

    <td class="total" colspan="3">Total: </td>
    <td><input type="text" name="total" value="0" readonly="readonly" size="8" class="cur" /></td>
    <td class="label">First Name: </td>
    <td colspan="3"><input type="text" name="first_name" value="" size="36" /></td>
    <td class="label">Last Name: </td>
    <td colspan="3"><input type="text" name="last_name" value="" size="36" /></td>
    <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>
    <td class="label">Phone: </td>
    <td class="last" colspan="3"><input type="text" name="phone" value="" size="36" /></td>
    <td class="submit" colspan="4"><input type="submit" name="submit" value="Submit" /></td>


  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Northern Virginia, United States
    Rep Power

    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.

    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
    Madgaon, Goa, India
    Rep Power

    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.

    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
    Rep Power

    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