JavaScript - Javascript E-commerce Site....passing The Shopping Cart Thru All The Pages.
Similar TutorialsHello guys my question has changed, i dont want to double post so here is my new question, I now have 3 arrays name price quantity totalPrices now i want to create a table that will hold them but it will be created when you click the shop cart button, this button will take you to a blank page with the table on it i would like something like this Toy name| Quantity | Price | Total price Toy1 | Quantity | Price | Total price of that toy Toy2 | Quantity | Price | Total price of that toy Toy3 | Quantity | Price | Total price of that toy ----- | -------- | ----- | Total price all together I have looked at many tutorials but i cant get any to work, all they do is draw nothing and i cant get it to work I am working on my shopping cart page with DOM. In my html I created two tables one for items with add buttons, and the other one for displaying how many items are added. So, here is my html codes Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta name="description" content="forms" /> <meta name="keywords" content="Client Side Programming, JavaScript, CSS" /> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <meta http-equiv="Content-Language" content="en-us" /> <script type="text/javascript" src="shopping.js"></script> </head> <body> <h1>Central Valley Chocolates</h1> <h2>Gourmet Chocolates</h2> <table border="1" id="chocoateTrable"> <tr id="ch1"> <td>Chocolate Truffles</td><td>$34.99</td><td> <input type="button" value="Add" onclick="addItem(document.getElementById('ch1').rowIndex)" /></td></tr> <tr id="ch2"> <td>Pecan</td><td>$14.99</td><td> <input type="button" value="Add" onclick="addItem(document.getElementById('ch2').rowIndex)" /></td></tr> <tr id="ch3"> <td>Truffles</td><td>$28.99</td><td> <input type="button" value="Add" onclick="addItem(document.getElementById('ch3').rowIndex)" /></td></tr> <tr id="ch4"> <td>caramel</td><td>$22.99</td><td> <input type="button" value="Add" onclick="addItem(document.getElementById('ch4').rowIndex)" /></td></tr> <tr id="ch5"> <td>Bark</td><td>$17.99</td><td> <input type="button" value="Add" onclick="addItem(document.getElementById('ch5').rowIndex)" /></td></tr> </table> <h2>Your Shopping Cart</h2> <table id="shoppingCart" border="1"> <tr><td>Your shopping cart is empty</td></tr> </table> <p id="total"> </p> </body> </html> And in my js file, I have two functions one for adding items and the other for deleting items. Code: var emptyCart = true; var salesTotal = 0; var curRow = 1; function addItem(selectedItem){ if(emptyCart == true){ document.getElementById('shoppingCart').deleteRow(0); emptyCart = false; } var curItem = document.getElementById("chocolateTable").rows[selectedItem].cells; var selectedItem = curItem[0].innerHTML; var itemPrice = curItem[1].innerHTML; var lastItem = document.getElementById("shoppingCart").rows.length; var cartTable = document.getElementById("shoppingCart"); var newRow = cartTable.insertRow(lastItem); document.getElementById("shoppingCart").rows[lastItem].id = "R" + curRow; var itemCell = newRow.insertCell(0); itemCell.innerHTML = selectedItem; var priceCell = newRow.insertCell(1); priceCell.innerHTML = itemPrice; var actionCell = newRow.insertCell(2); actionCell.innerHTML = "<input type='button' value = 'Remove' " + "onclick=\"removeItem('R"+curROw + "')\" />"; ++curRow; salesTotal += parseFloat(itemPrice.substring(1)); document.getElementById('total').innerHTML = "<strong>Sales total</strong>: $" + salesTotal.toFixed(2); } function removeItem(rowNum){ if(document.getElementById("shoppingCart").rows.length == 1){ document.getElementById("shoppingCart").rows[0].cells[0].innerHTML = "<td>Your shopping cart is empty</td>"; document.getElementById("shoppingCart").rows[0].cells[1].innerHTML = "<td>$0.00</td>"; document.getElementById('total').innerHTML = "$" + salesTotal.toFixed(2); emptyCart = true; } else{ var selectedRow = document.getElementById(rowNum).rowIndex; var itemPrice = document.getElementById("shoppingCart").rows[selectedRow].cells[1].innerHTML; document.getElementById("ShoppingCart").deleteRow(seletedRow); salesTotal = salesTotal - parseFloat(itemPrice.substring(1)); document.getElementById('total').innerHTML = "$" + salesTotal.toFixed(2); } } However, when I click add button to add each item, I don't see anything in my shoppingCart. Anyone can tell me what is wrong with my code? Hi and hope someone can help, I'm doing some course work on setting / getting cookies for a shopping cart. It basically works except the user can only select one item of each of the goods on offer i.e. you can only buy one cup (not two), one saucer etc. etc. I've deliberately not put a quantity field on the goods selection page and was hoping to modify my set cookie function so that if you've already selected one cup, the next tiume you select a cup, cups become 2 etc. etc. Here's my set cookie function. Notice that I've hard wired a "1" in there which I want to change to the variable for the number of cups. I've also commented out some of the first few lines which were experiments to resolve the problem. Code: function setCookie(name) { alert("Thank you.\n\nYour basket has been updated."); /* if(name.value !=0 || name.value==null) name.value++; x=getCookie(name) || 0; alert(document.cookie);*/ var today = new Date(); var expiry = new Date(today.getTime()+28*24*60*60*1000); // plus 28 days document.cookie=name+"= 1 ;expires="+expiry.toGMTString(); cart = document.cookie; } My thanks, R A little while later... I've cracked it, my function now looks like this and it appears to work... Code: function setCookie(name) { alert("Thank you.\n\nYour basket has been updated."); x=parseInt(getCookie(name)) || 0; alert(x); y=x+1; var today = new Date(); var expiry = new Date(today.getTime()+28*24*60*60*1000); // plus 28 days document.cookie=name+"="+y+";expires="+expiry.toGMTString(); cart = document.cookie; } Thanks you fror your interest, appreciated, R I'm using simpleCart.js (http://simplecartjs.com ) on http://yogasports.biz and have run into issues. I would like the checkout page to allow items to be removed or quantities edited and it is currently not allowing it. The javascript is located at http://yogasports.biz/simpleCart.js Also there are additional shipping options that are not appearing upon checkout yet they are declared in the paypal account. I'm unsure of what to edit in the javascript file so that they will appear. Hi all, I have just joined this forum hoping for some help on a problem i am having, i have a feeling the solution is simple but im a noob at JAVA so i have no idea how to work it out. The site is.... http://inspirecalendar.net/html/purchase.html The problem is the following.... When you select US (West Coast) as your location it doesn't update the price on the right properly. If you select it first it just won't display anything. if you select it after choosing another one and hitting continue then it doesn't change. Any help would be appreciated, hope i explain the problem properly if you have any questions feel free to ask... Thnks in advance :Smiley Hi Guys, I am trying to get the Fat-Free shopping cart woking with a flash file. I have tried [CODE] on (rollOver) { _parent.gotoAndPlay("s1"); } on (releaseOutside, rollOut) { _parent.gotoAndPlay("s2"); } on (release) {getURL("https://www.e-junkie.com/ecom/fgb.php?c=cart&cl=1&ejc=2&merchant_id=your_google_merchant_id&business=website.com" target="ej_ejc" class="ec_ejc_thkbx" onClick="javascript:return EJEJC_lc(this));"}[CODE] The compiler tells me that a ')' or ',' is expected. Any help appreciated. I am a newbie. Thanks PeterW Hi All! Thanks in advance to any and all who help me. What we are trying to do is have a form where customers can use a conditional logic based form that upon submission will redirect to checkout with an appropriate product bundle being populated in the shopping cart. I have some experience with creating forms, in fact I have made a decent form that I have modified from an online form builder... the problem is I have no idea how to auto-fill the shopping cart... period. From what I have read in my research this should be a function of Javascript. I have a decent working knowledge of php, and html, but I am pretty clueless when it comes to Javascript, if anyone could point me in the right direction for this project I would really appreciate it. hello recently i finally managed to make a working shopping cart system, in that i can add as many items as i want and it will work, all using cookies. Then i have another page, the cart which reads the cookie entry made with each demo purchase, then using a while loop lists these items, the problem i am having, is that when i add the textbox which would hold the value of the quantity, it is put out on the next line down, instead of next to the text which i find quite annoying. Also i would like to know how i can add a button next to the textbox, for updating the quantity, also ide like to know how i could access that button, or have it do something, since it wouldnt be prewritten. any help would be greatly apreciated. here is a zip file containing the html files of the customizable purchase page, and the new shopping cart. be sure to refresh the page before trying to use the first page, so it establishes the counter cookie. I have the following snippet of HTML code: Quote: <table id="identificativo"> .......... .......... .......... <tr id="id_riga"> <td width="40%"> Cella 1 </td> <td> <input type="hidden" name="params" value="valore1 valore2" /> <input type="button" value="Add/Change" onclick="return del()" /> </td> <td> Cella3 </td> </tr> ............ ............ ............ and the following javascript code that replace the second row of table with new form: Quote: function del() { if (document.getElementById) { var tabella = document.getElementById("identificativo"); tabella.deleteRow(1); x = tabella.insertRow(1); x.innerHTML = "<td colspan='3'> \ <form action='/cgi-bin/save.cgi' method='POST'> \ <input type='text' name='name' size='30' /> \ <input type='submit' value='Save' /> \ </form> \ </td>"; } } How can to pass hidden values valore1 and valore2 from old cell of row to new form with javascript? Thank you in advance to answers, savio Right now this javascript shopping cart works to 1. charge shipping based on the item, 2. the destination country, 3. it combines shipping for a quantity over 1 of the SAME item. Each item has 2 different possible shipping charges. I am trying to get the javascript to check the shopping cart to see what item in the cart has the highest possible shipping charge, charge that amount to that item, and charge the lowest possible shipping charge on all other items in the cart. If item A is purchased alone shipping is $5.00. If purchased with item B, which costs $10.00 to ship alone, the $10.00 is charged for item B and only $3.00 for item A. Because item B had the higher shipping charge at a quantity of one. I have tried adding various things like me.items[current], item.shipping, me.shipping, this.shipping, shipping_Cost, and other things next to the second && in the part of the script that shows the country. I have also tried adding && if (me.whatever) and && if (item.whatever) and similar things at the beginning of the script next to if (this.country). I have found the parts of the script that pertain to cart items and to updating the shopping cart. Now I am stuck. The javascript is in 2 parts. One part goes in the item page, which I will post first. The second part goes in an external javascript file which I will post at the bottom. In between there is the part that shows the shopping cart. It isn't part of the javascript. Code: <script type="text/javascript" src="simpleCart.js"></script> <script type="text/javascript"> <!-- simpleCart.checkoutTo = PayPal; simpleCart.email = "my Paypal email address"; simpleCart.cartHeaders = ["Name" , "Price" , "Quantity" , "remove" ]; CartItem.prototype.shipping=function(){ // we are using a 'country' field to calculate the shipping, // so we first make sure the item has a country if(this.country){ if( this.country == 'United States' && this.quantity == '1'){ return this.quantity*5.00; } else if( this.country == 'United States' && this.quantity >= '2') { return this.quantity*3.00; } else if( this.country == 'Belgium' && this.quantity == '1') { return this.quantity*12.00; } else if( this.country == 'Belgium' && this.quantity >= '2') { return this.quantity*9.00; else { return this.quantity*0.00; } } else { // use a default of $0.00 per item if there is no 'country' field return this.quantity*0.00; } } // --></script> Code: <div style="display:block;"></div> <div>SHOPPING CART</div> <div class="cartHeaders"></div><br><br><br> <div class="simpleCart_items"></div> <div id="totals"> Item Total: <span class="simpleCart_total"></span><br>Shipping Total: <span class="simpleCart_shippingCost"></span><br>Tax: <span class="simpleCart_taxCost"></span><br>Final Total: <span class="simpleCart_finalTotal"></span> </div> <br><br><br><br> <br><br> <a href="javascript:;" class="simpleCart_empty">Empty Shopping Cart</a><br><br> <a href="javascript:;" class="simpleCart_checkout">Checkout Through Paypal</a> </div></div> separate javascript file is here http://simplecartjs.com/documentation.html Good morning! Hopefully somebody can help me with this javascript question. (No knowledge of Google Anayltics needed for this.) I'm adding Google Analytics 'Event Tracking' javascript to a typical 'Add to Cart' button. The customer enters desired quantity in the text box and clicks the 'Add to Cart' button to order. I would like to use the Event Tracking javascript to record the button click - plus the Value entered in the text box - as one event. Here's the code for the form (the text box and 'Add to Cart' button code lines are bold): Code: <form method="post" action="http://www.yoursite.com/mm5/merchant.mvc?"> <input type="hidden" name="Screen" value="BASK" /> <input type="hidden" name="Product_Code" value="yourproductcode" /> <input type="hidden" name="Action" value="ADPR" /> <label for="quantity">Quantity:</label> <input type="text" name="quantity" value="0" /> <input type="submit" value="Add To Cart" /> </form> I came up with this javascript for the Event Tracking (to be inserted somewhere above): Code: onClick="_gaq.push(['_trackEvent', 'OrderForms', 'AddToCart', 'yourproductcode', document.getElementByName('quantity').value]);" I'm wondering where I would enter this javacript code inside the form code to pick up both the button click - plus the quantity? You'll notice that I'm using 'getElementByName' to extract 'quantity' from the text box and record its 'value'. If I enter this javascript on the 'Add to Cart' line - right after "Add to Cart", would 'quantity' still get picked up upon button-click - since it's all on the same form? Or would it be a problem because 'quantity' is contained in a different <input> container? Thanks! Any help is greatly appreciated. IB I am using an e-commerce solution to run my web business. The checkout portion of the website is COMPLETELY closed off to me and I can't access the files. What I need: A javascript example / solution that will prevent someone from submitting a form without first filling in a field. What I currently have is a series of radio buttons built into the checkout process. It displays 20 options and the customer has to choose which one of the twenty options applies to them. Each customer only has 2 valid options to choose from based on their zip code, but currently the other zip code shipping options are listed as well. I have built a javascript that works great hiding all of the shipping options, taking data from the customer, and then only showing the correct shipping option. The problem is, if the customer doesn't fill this out, then they can just hit submit and pay the default shipping option without ever seeing that they didn't choose the correct zip code. What can I do to prevent them from submitting the form? The submit form is a button and there are 6 other buttons and none of them have any identifying factors. Hi, I'm in year 11 studying GCSE Computing and I'm working on gathering some primary research for a project. I'd be really grateful if someone with experience in web developing would be able to explain to me some of the uses of JavaScript within web pages- what have you used it for specifically? Also, any examples of how JavaScript is used outside of web pages would be really helpful. Thank you! Hello, I'm trying to pass a query from my URL into a <script> using Javascript. THe URL would be formatted like this http://www.FakeSite.com/index.html?zip=12345 The script i want to pass the 12345 into is Code: <script type="text/javascript"> /* / Full Data - Form + TY Page Clicks */ var MediaAlphaExchange = { "type": "ad_unit", "placement_id": "ijX_k5FQsJfYjjaLLbfeJcKzT7NQow", "version": "17", "sub_1": "test sub id", "data": { "zip": "90210" } }; </script> <script src="//insurance.mediaalpha.com/js/serve.js"></script> The 90210 should automattically turn into 12345 Can someone show me the exact code to use to make this happen? Reply With Quote 12-29-2014, 03:12 AM #2 sunfighter View Profile View Forum Posts Senior Coder Join Date Jan 2011 Location Missouri Posts 4,830 Thanks 25 Thanked 672 Times in 671 Posts This is a bit of trickery Code: <!DOCTYPE html> <html> <head> <title>New document</title> </head> <body> <form name="joe"> <input type="hidden" name="burns"> </form> <script> /* These 3 lines put the url of this page into the hidden field of a form no one can see */ var locate = window.location; document.joe.burns.value = locate; var text = document.joe.burns.value; alert(delineate2(text)); // This line calls the next function and shows you what it returns. You should be able to sub this to put the return into your code. function delineate2(str){ point = str.lastIndexOf("="); return(str.substring(point + 1, str.length)); } </script> </body> </html> Hello Guys, I have a page with thumb nails and a large image that changes when clicking on thumbnail. I need to display dynamic title for the image. It wont display because the page is not reloading.. How do you pass the name so it will show up without reloading? Here is the JS for switching dynamically loading the large image PHP Code: <script language="javascript"> $(document).ready(function() { $('.thumbnail').live("click", function() { $('#mainImage').hide(); $('#atitle').hide(); $('#imageWrap').css('background-image', "url('/images/ajax-loader.gif')"); var i = $('<img />').attr('src',this.href).load(function() { $('#mainImage').attr('src', i.attr('src')); $('#imageWrap').css('background-image', 'none'); $('#mainImage').fadeIn(); $('#atitle').fadeIn(); }); return false; }); }); </script> Here is the link for the thumb PHP Code: // Thumbnail Display echo '<a href="artists/'.$artist.'/'.$imgname.'.jpg" class="thumbnail"><img src="artists/'.$artist.'/previews/'.$imgname.'.jpg" border="0" /></a>'; [/PHP] Link to the large image PHP Code: // Large image display echo '<img src="artists/'.$artist.'/'.$first_image.'.jpg" alt="Main Image" id="mainImage"/>'; Here is the php echo for the title PHP Code: <? echo str_replace('"', '`', $iname); ?> I need to some how pass a php var from the thumb link to the js and have it display dynamically(without reloading) Hope I explained this correctly.. Please advise.. Thanks, Dan Hi, I want to redirect pages e,g 'http://mydomain.com/pagename.html' to 'http://www.mydomain.com/pagename.html",i.e if anyone access without www then it will be added automatically,how can i do this using javascript? plz help Thanks |