javascript - Pushing to null array, even though it is called as a global and works in my localhost, but not on the server -
i'm getting following error in javascript. title states, code working on localhost, not when put onto server
uncaught typeerror: cannot read property 'push' of null @ additemtocart (shoppingcart.js:36) @ htmlbuttonelement.<anonymous> (shoppingcart.js:193) @ htmldocument.dispatch (jquery-1.11.1.min.js:3) @ htmldocument.r.handle (jquery-1.11.1.min.js:3)
i have identified before push function called cart variable indeed null, declare globally empty array, i'm not sure why be.
code function failing on
var cart = []; function additemtocart(name, price, count, id, shortname) { (var in cart) { if (cart[i].id === id) { cart[i].count += count; savecart(); (var in cart) { if (cart[i].id == 500 && id != 500) { removeitemfromcart(500); alert('because changed cart, have reapply coupon'); } } return; } } (var in cart) { if (cart[i].id == 500 && id != 500) { removeitemfromcart(500); alert('because changed cart, have reapply coupon'); } } var item = new item(name, price, count, id, shortname); console.log(cart); cart.push(item); savecart(); }
the error happens @ teh cart.push(item); line because cart null , can not pushed to. anymore information may need feel free , shoot way.
thanks in advance!!!
edit:
function displaycart() { console.log("*** display cart ***"); var cartarray = listcart(); var output = "<tr><th>items</th><th>quantity</th><th>price</th></tr>"; var output2 = "<tr><th> </th><th>product name</th><th>product price</th><th>quantity</th><th>total</th></tr>"; var output3 = " <tr><th>product(s)</th></tr>"; var output4 = ""; console.log(listcart()); (var in cartarray) { output += "<tr class='item'><td><div class='delete' id='removeitem' data-id='" + cartarray[i].id + "'></div>" + cartarray[i].name + "</td><td><input type='text' value='" + cartarray[i].count + "' readonly></td> <td class='price'>" + cartarray[i].price + "</td></tr>" output2 += "<tr class='item'>" + "<td class='thumb'><a href='" + cartarray[i].id + "-item.php'><img src='img/catalog/product-gallery/" + cartarray[i].id + ".png' alt='product image'/></a></td>" + "<td class='name'><a href='" + cartarray[i].id + "'-item.php'>" + cartarray[i].name + "</a></td>" + "<td class='price'>$" + cartarray[i].price + "</td>" + "<td class='qnt-count'>" + "<a class='incr-btn' href='#' id='oneless' data-id='" + cartarray[i].id + "'>-</a>" + "<input class='quantity form-control' type='text' value=' " + cartarray[i].count + " '>" + "<a class='incr-btn' id='onemore' data-productid='" + cartarray[i].id + "' data-name='" + cartarray[i].name + "' data-quantity='" + cartarray[i].count + "' href='#'>+</a>" + "</td>" + "<td class='total'>$<em id='test'>" + cartarray[i].total + "</em></td>" + "<td class='delete' id='removeallfromcart' data-id='" + cartarray[i].id + "'><i class='icon-delete'></i></td>" + "</tr>"; output3 += " <tr><td class='name border'>" + cartarray[i].shortname + "<span>x" + cartarray[i].count + "</span></td>" + "<td class='price border'>$" + cartarray[i].total + "</td></tr>"; if ($("#offercount").attr("data-id") == cartarray[i].id) { output4 += +"<a class='incr-btn' href='#' id='oneless' data-id='" + cartarray[i].id + "'>-</a>" + "<input class='quantity form-control' type='text' value=' " + cartarray[i].count + " '>" + "<a class='incr-btn' id='onemore' data-productid='" + cartarray[i].id + "' data-name='" + cartarray[i].name + "' data-quantity='" + cartarray[i].count + "' href='#'>+</a>"; } } output3 += " <tr><td class='th border'>shipping</td><td class='align-r border'>free shipping</td></tr>" + "<tr><td class='th'>order total</td><td class='price'>$" + totalcart() + "</td></tr>" $("#offercount").html(output4); $("#productdisplay").html(output3); $("#showfullcart").html(output2); $("#showcart").html(output); $("#carttotal").html(totalcart()); $("#totalcart").html(totalcart()); $("#mycarttotal").html(totalcart()); $("#showmytotal").html(totalcart()); $("#cartcount").html(countcart());
}
function addcoupontocart(coupon) { if (coupon == 'coupon10' && couponsadded == 0) { var couponreduce = -(totalcart() * .1).tofixed(2); additemtocart('10% off coupon', couponreduce, 1, 500, '10% off'); couponsadded += 1; savecoupon(); } displaycart();
}
function countcart() { var totalcount = 0; (var in cart) { totalcount += cart[i].count; } return totalcount;
}
function removeitemfromcartall(id) { (var in cart) { if (cart[i].id === id) { cart.splice(i, 1); break; } } (var in cart) { if (cart[i].id == 500 && id != 500) { removeitemfromcart(500); alert('because changed cart, have reapply coupon'); } } savecart();
}
code calls addcoupontocart function whenever post gets set.
<?php if (isset($_post['coupon_code'])) { ?> <script> addcoupontocart(coupon); </script> <?php } ?>
@codenoname provided correct answer of checking null cart. solved issue, lot of functions not being defined properly. had wrapped entire code in document ready function seemed issue. whenever removed worked. thank input.
if (!cart) { cart = []; }
Comments
Post a Comment