calculating total by updating quantity - JS AND AS

# Thread: calculating total by updating quantity - JS AND AS

1. Senior Member
Join Date
Dec 1969
Posts
468

## calculating total by updating quantity - JS AND AS

When a customer changes quantity in shopping cart, can price field be updated by event handler like onblur? I don&#039;t want to show recalculate button to do this. With Bill&#039;s help I&#039;ve done something where when cutsomer enteres somethign in one field total gets updated in total, but I&#039;m not sure how to pass price for quantity which was updated? <BR><BR>My cart looks like: (To show code in this post, I&#039;ve tak out some code like recordset etc..so that code doesn&#039;t get long)<BR><BR>for i = 1 to ubound(carti) <BR>Response.Write "Productname" & pname<BR>Response.Write FormatCurrency(rs("price"))<BR>price = rs("price") * cartq(i)<BR>subtotal = subtotal + price<BR>Response.Write &#060;input type=text size=2 name=qty"& i &" value=" & cartq(i) <BR>next<BR><BR>Response.Write "Subtotal" & formatcurrency(subtotal) <BR><BR>Thanks in advance<BR>Any pointer will be appreciated,

2. Senior Member
Join Date
Dec 1969
Posts
96,118

## Well, I'd toss that code...

...and start again.<BR><BR>How do you expect to coordinate the cartq() array and the RS recordset??? How can you be sure they will stay in sync?<BR><BR><BR><BR>

3. Senior Member
Join Date
Dec 1969
Posts
468

## RE: Well, I'd toss that code...

As I&#039;ve all items in cart stored in array, We can use that subscript to trace quanity and price? <BR><BR>for i = 1 to ubound(carti)<BR>set rs=conn.execute("SELECT * from Products where Prodid = " & cint(carti(i)) )<BR>prodname = rs("prodname")<BR> unitprice = rs("saleprice")<BR> price = unitprice * cartq(i)<BR>Response.Write "&#060;tr bgcolor=" & bcolor & "&#062;&#060;td align=left&#062;&#060;font face=arial size=1&#062;&#060;a href=cart2.asp?index="& i &"&#062;&#060;image src=images/b-del.gif border=0 align=left&#062;&#060;/a&#062;&#060;/td&#062; "<BR><BR>Response.Write "&#060;td valign=top&#062;&#060;font face=arial size=1&#062;&#060;b&#062;&#060;a href=""proddetail.asp?pid=" & carti(i) &"""&#062;" & prodname & "&#060;/a&#062;td&#062;"<BR><BR>Response.Write "&#060;td align=right&#062;&#060;input type=text size=2 name=qty"& i &" value=" & cartq(i) & "&#062;&#060;/td&#062;"<BR>Response.Write "&#060;td align=right&#062;&#060;b&#062;" & FormatCurrency(unitprice) & "&#060;/td&#062;"<BR>Response.Write "&#060;td align=right&#062;&#060;b&#062;" & FormatCurrency(price) & "&#060;/td&#062;"<BR>subtotal = subtotal + price<BR>next<BR>Response.Write "&#060;tr bgcolor=white&#062;&#060;td colspan=4 align=right&#062;&#060;font face=arial size=1 &#062;Subtotal&#060;/td&#062;"<BR>Response.Write "&#060;td align=right&#062;&#060;font face=arial size=1 color=black&#062;&#060;b&#062;" & FormatCurrency(total) & "&#060;/td&#062;"<BR> Response.Write "&#060;/tr&#062;"<BR>%&#062;&#060;/table&#062;<BR> <BR><BR><BR>Thanks!!!

4. Senior Member
Join Date
Dec 1969
Posts
96,118

## That's fine, but...

...don&#039;t mix the array usage with *LATER* RS usage. How do you know somebody didn&#039;t slip a new product in on you in the meantime, so that they no longer match up? Why do you assume that just because "widget" was the 4th item in the cart to begin with that it will remain the 4th item you get in the recordset?<BR><BR>If you are going to keep quantity, etc., in an array (is this a SESSION arrray???) then you should keep the other info there, as well.<BR><BR>

5. Senior Member
Join Date
Dec 1969
Posts
468

6. Senior Member
Join Date
Dec 1969
Posts
96,118

## Ahhh...then you should...

...structure the cookie as an array of arrays, I think.<BR><BR>That is, have each item in the cart represented as:<BR><BR> prodid#prodname#price#qty<BR><BR>and then separate the items with the comma, as you are doing<BR><BR><BR>So you&#039;d have something like:<BR> 773#red widget#7.99#3<BR><BR>But I have to ask... Do you carry all the items along in the cart, even if the user asked for a quantity of ZERO for that item???<BR><BR>

7. Senior Member
Join Date
Dec 1969
Posts
468

## RE: qty is set to 1

so when one adds item in the cart quantity in always added as 1. as values has been set to 1.

8. Senior Member
Join Date
Dec 1969
Posts
96,118

## You misunderstand me...

Look, you were trying to coordinate the display of info from the cart (quantity, for example) with info from a recordset, right?<BR><BR>As in your code that was doing<BR> price = rs("price") * cartq(i)<BR><BR>Now, HOW DO YOU KNOW that the rs("price") you are using there is the RIGHT ONE for the particular cart item you are currently working on?????<BR><BR>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•