SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Nov 2003
    Location
    Australia
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem displaying a grand total in some browsers. Opera 7 is fine.

    I've got this script that someone else wrote and I've been modifying it to suit my needs. I was using Opera 7 to test the script and everything seemed normal. Then I tested it in other browsers and I started to get NaN instead of grandTotal. Why does Opera 7 display the grand total fine and every other engine doesn't? I haven't done much JavaScript before and from my reading of NaN, I still can't work out what's wrong with the script. The script is supposed to print an invoice with details passed from another html file via a form. Any help? Thanks.

    <script language="JavaScript" type="text/JavaScript">
    function decodeString() {
    valNum = new Array();
    valData = new Array();
    var string, length, dataCount, orderData, grandTotal;
    string = "" + unescape(location.search);
    string = string.substring(1,string.length);
    length = location.search.length;
    orderData = "";
    dataCount = 1;
    for (var c = 0; c < string.length; c++)
    if (string.charAt(c).indexOf("&") != -1) dataCount++;

    orderData = "<table border=0 width='80%' style='border:1px solid #000' cellpadding='3'>";
    orderData += "<tr><td style='border-bottom:2px solid #000'><strong>Description</strong></td><td style='border-bottom:2px solid #000'><strong>Cost in AUD</strong></td></tr>";
    grandTotal = 0;
    for (var i = 0; i < dataCount; i++)
    {
    valNum[i] = string.substring(0,string.indexOf("="));
    string = string.substring(string.indexOf("=")+1,string.length);
    if (i == dataCount-1) valData[i] = string;
    else valData[i] = string.substring(0,string.indexOf("&"));
    ampd = valData[i].indexOf("&");
    pipe = valData[i].indexOf("-");
    star = valData[i].indexOf("*");
    line = valData[i].indexOf("$");
    itemnum = string.substring(0,pipe);
    itemdsc = string.substring(pipe+1,star);
    itemcst = string.substring(star+1,line);
    string = string.substring(ampd+1,string.length);

    /* remove underscores */
    itemdsc = itemdsc.replace(/_/g," ");
    orderData += "<tr>";
    orderData += "<input type=hidden name=item" + (i+1) + "dsc value='" + itemdsc + "'>";
    orderData += "<input type=hidden name=item" + (i+1) + "cst value='$" + itemcst + "'>";
    orderData += "<td style='border-bottom:2px solid #000'>" + itemdsc + "</td>";
    orderData += "<td style='border-bottom:2px solid #000'>" + itemcst + "</td>";
    orderData += "</tr>";
    grandTotal += parseFloat(itemcst);
    }
    /* round decimal points to two points only */
    var original=grandTotal;
    var result=Math.round(original*100)/100
    orderData += "<tr>";
    orderData += "<td align=left><strong>Total</strong></td><td>" + result + "</td>";
    orderData += "</tr>";
    orderData += "<tr>";
    orderData += "</tr>";
    orderData += "</table>";

    document.write(orderData);

    }

    // End -->
    </script>

  2. #2
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not 100%, as I gave the code only a cursory look, but the problem is most likely here

    grandTotal += parseFloat(itemcst);

    If parseFloat doesn't find a number, it will return NaN. The script isn't very semantic, so I don't know what itemcst is supposed to be. Item cost? The part of the script that splits up the data might be failing if that's the case.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev





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
  •