2

Earlier I had an issue with transferring data between html pages but I luckily got help. However, I'm having some trouble displaying certain prices taken from a shopping cart page, and displaying them in the checkout page. They way I am doing this is with local browser storage and I was informed that this can be done with multiple instances, but I must be missing something.

//table.html
<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8"/>
<style>
table, th, td {
  border: 1px solid black;
}
</style>
</head>

<body>

<h2>Html Table</h2>

<table id="EstoreTable" style="width:100%">
  <tr>
    <th>Item Name</th>
    <th>Quantity</th> 
    <th>Price</th>
  </tr>
  <tr>
    <td>iPhone X</td>
    <td>10</td>
    <td>299.99</td>
  </tr>
  <tr>
    <td>Apple Watch 3</td>
    <td>12</td>
    <td>250.00</td>
  </tr>
  <tr>
    <td>MacBook Pro 13" </td>
    <td>7</td>
    <td>1199.99</td>
  </tr>
</table>
    <a href="assign1_agm75_checkout.html" target="_blank" id="checkout">Checkout</a>
    <script src="CheckoutAmounts.js"></script>
    </body>  

</html>
//js file
function getCheckoutAmounts()
{ 
    var table = document.getElementById('EstoreTable'); //data from TestTable.html
    var sum = 0,
        taxTotal = 0,
        tax = .0825,
        shipping = 5.99,
        due = 0;

    for (var i = 1; i < table.rows.length; i++)
        {
           sum = sum + parseFloat(table.rows[i].cells[2].innerHTML); //stores total price into sum
        }

    taxTotal = tax * sum; //stores calculated tax

    due = sum + taxTotal + shipping;

    // SET VIA query string
    this.href += "?sum=" + sum;
    this.href += "?taxTotal=" + taxTotal;
    this.href += "?shipping=" + shipping;
    this.href += "?due=" + due;

    // SET SUM VIA localStorage
    window.localStorage.setItem("sum", sum);
    window.localStorage.setItem("taxTotal", taxTotal);
    window.localStorage.setItem("shipping", shipping);
    window.localStorage.getItem("due", due);

}

document.querySelector("#checkout").addEventListener("click", getCheckoutAmounts);

//Checkout.html
<html>
<h1>Checkout</h1>

Total Shopping Amount: $<span id="Shopamount"></span><br>

Total Tax: $<span id="TaxTotal"></span><br>

Total Shipping Charges: $<span id= "charges"></span><br>

Total Amount Due: $<span id="total"></span><br>
<script>

    // GET data VIA localStorage
    document.querySelector("#Shopamount").textContent = window.localStorage.getItem("sum");
    document.querySelector("#TaxTotal").textContent = window.localStorage.getItem("taxTotal");
    document.querySelector("#charges").textContent = window.localStorage.getItem("shipping");
    document.querySelector("#total").textContent = window.localStorage.getItem("due");


    // GET SUM VIA query string
    let queryString = document.location.search;
    let sum = queryString.split("=")[1];
    let taxTotal = queryString.split("=")[1];
    let shipping = queryString.split("=")[1];
    let due = queryString.split("=")[1];

    document.querySelector("#Shopamount").textContent = sum;
    document.querySelector("TaxTotal").textContent = taxTotal;
    document.querySelector("charges").textContent = shipping;
    document.querySelector("total").textContent = due;
</script>
Total Shopping Amount: $ $1749.98?taxTotal
Total Tax: $144.37335000000002
Total Shipping Charges: $5.99
Total Amount Due: $

However, when I run the JS, this is my result in checkout.html .There are other parts to the checkout.html so that's why there's no closing tag.

Asa Murphy
  • 71
  • 7
  • What's the error that you get? – SRR Oct 13 '19 at 20:56
  • The results that display on the checkout page are as follows: Total Shopping Amount: $ Total Tax: $144.37335000000002 Total Shipping Charges: $5.99 Total Amount Due: $ @S.Ramjit – Asa Murphy Oct 13 '19 at 20:59
  • You can edit your question to put in the results on the checkout page instead of having it in a comment btw – SRR Oct 13 '19 at 21:02
  • Also, in ```getCheckoutAmounts()``` you have ```window.localStorage.getItem("due", due);``` .Should that be get or set? – SRR Oct 13 '19 at 21:05
  • @S.Ramjit Sorry, I made the edit. Thanks – Asa Murphy Oct 13 '19 at 21:06
  • @S.Ramjit it should be set my apoligies, I just noticed that – Asa Murphy Oct 13 '19 at 21:07
  • @S.Ramjit Wow! that actually fixed part of the display issue, I guess now would be how to get rid of the ?taxTotal and then reducing the amount of integers behind the decimal to 2. I made an update of what the current output is – Asa Murphy Oct 13 '19 at 21:12
  • 1
    Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/200788/discussion-between-s-ramjit-and-asa-murphy). – SRR Oct 13 '19 at 21:13
  • 2
    Possible duplicate of [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Jared Smith Oct 13 '19 at 21:25
  • @JaredSmith I already got that part fixed, but I really appreciate the help. All that's left is this pesky ?taxTotal after the first result – Asa Murphy Oct 13 '19 at 21:29
  • do not use `queryString.split("=")[1]`. Use https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams#Browser_compatibility see: https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript – Beniamin H Oct 20 '19 at 23:07

0 Answers0