I'm working on an exercise I need to have finished pretty soon. The concept is simple: online bookstore with cart etc. It's written on php, using JS and mysql.
Each time I view a book's details, I can add it to my cart, where I can also alter it's quantity.
The code for my cart is this:
As you can see, each time I choose a book to add it to my cart, a new span will be created, to hold the info of this book. What I need to do with the ManageItem js function, is to update the total cost at the bottom of the page, but I can't seem to be getting anywhere, so any help will be mostly appreciated.
Here is what I've done so far:
Code:
function ManageItem(x){
var my_spans = document.getElementsByTagName("span");
var my_prices = new Array();
var my_quantity = new Array();
var total = 0;
for(var i=0;i<my_spans.length;i++){
//working with each span seperate
var this_span = my_spans[i].childNodes;
my_quantity[i] = x.selectedIndex.value;
my_prices[i] = parseInt(this_span.document.getElementById("price").value)*parseInt(my_quantity[i]);
}
for(var j=0;j<my_prices.length;j++){
total +=my_prices[j];
}
document.getElementById("total").value = total;
}
function ManageItem(){
var my_spans = document.getElementsByTagName("span");
var this_span = new Array();
var my_prices = new Array();
var my_quantity = new Array();
var total = 0;
for(var i=0;i<my_spans.length;i++){
this_span[i] = my_spans[i].getElementsByTagName("*");
for(var j=0;j<this_span[i].length;j++){
my_quantity[i] = this_span[i].item(2).selectedIndex.value;
my_prices[i] = parseInt(this_span[i].item(1).value)*parseInt(my_quantity[i]);
}
}
for(var j=0;j<my_prices.length;j++){
total +=parseInt(my_prices[j]);
}
document.getElementById("total").value = total;
}
But if i try to print my my_quantity[i] to check if the value is assigned correctly, I'm getting "undefined" :/
What you might want to do is use a <textarea> to store your cart information.
We all have baggage to carry in life, unfortunately for me I always get the trolley with the wonky wheel...
Code:
Youre = {
STILL_not_getting_it:function(){
alert("YOU, the original poster / thread starter NEED to POST the code and NOT a LINK.");
},
MissingThePoint:function(msg){
alert("You're missing the point. " + msg);
}
}
Youre.STILL_not_getting_it();
For the beginning I see a mistake: you create, in a PHP loop, elements which have the same id (id="title", id="price"...). Unlike the name attribute, the id must be an unique identifier on the same document/session.
Bookmarks