 |
ASP Pro Code Clinic As of Oct 5, 2005, this forum is now locked. No posts have been deleted. Please use "Classic ASP Professional" at: http://p2p.wrox.com/forum.asp?FORUM_ID=56 for discussions similar to the old ASP Pro Code Clinic or one of the other many remaining ASP and ASP.NET forums here. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP Pro Code Clinic section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|

June 4th, 2004, 07:04 PM
|
Registered User
|
|
Join Date: Jan 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Problems with addition
Hi all,
I'm having serious problems with my commerce site, and I have no idea why. Currently, users are directed to a page titled detail.asp, where they select a price and a shipping / delivery preference. This form data (productPrice and shippingPrice) is transferred to my cart.asp page, which is a basic shopping cart.
I'm trying to read this the form data from the detail.asp page and add the two numbers together to figure out the final price of the item. However, when I try to add these numbers together, a concatenation occurs instead of addition. So, if I'm passing through 25 as the productPrice and 7 as the shippingPrice, my answer is 257. Last time I checked, the answer is 32. I'm using plus signs ( + ) to do the addition, but for some reason this keeps happening. I've attached the form from the detail.asp page as well as the full code for the cart.asp page. I've bolded what I think are the problem areas. Any help would be greatly appreciated!
FORM FROM DETAIL.ASP
<form method="post" action="cart.asp">
<input name="pid" type="hidden" value="<%=objRS("id")%>">
<input name="productName" type="hidden" value="<%=objRS("basketTitle")%>">
<select name="productPrice" class="formText3">
<%
Dim incrementPrice
incrementPrice = productPrice
'Start loop
do while incrementPrice <= 70
Response.Write "<option value = " & incrementPrice &"> $" & incrementPrice & ".00</option>"
incrementPrice = incrementPrice + 5
loop
%>
</select><br><br>
<img src="images/two.gif">Select shipping or delivery preference:<br><br>
<INPUT TYPE="radio" name= "shippingPrice" value= -1 checked><span class="itemTitle">: Ship this item <a href="shipping.asp">(how much is shipping?)</a></span><br>
<INPUT TYPE="radio" name= "shippingPrice" value= 6><span class="itemTitle">: $6.00 - Delivery to Allentown</span><br>
<INPUT TYPE="radio" name= "shippingPrice" value= 7.5><span class="itemTitle">: $7.50 - Delivery to Greater Lehigh Valley areas</span><br><br>
<INPUT TYPE="image" SRC="images/add_to_cart.gif" BORDER=0 ALT="Add Item to Cart">
</form>
CART.ASP PAGE
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>basketsbymandk.com</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="bbmk.css" rel="stylesheet" type="text/css">
</head>
<body topmargin=0 leftmargin=0 vlink="#550000" alink="#550000" link="#550000">
<table width="800" border="0" cellpadding="0" cellspacing="0" class="bodyText">
<tr>
<td colspan="3" valign="top"><img src="images/header_top_left.gif" width="440" height="133" border="0" usemap="#home"><img src="images/header_top_right.gif" width="360" height="133" usemap="#viewcart" border="0"></td>
</tr>
<tr valign="top">
<td width="166"></td>
<td width="634"><img src="images/your_shopping_cart.gif">
<table width="634" cellpadding="15" cellspacing="15" border="0">
<tr><td><form method="post" action="cart.asp">
<%
' Define Constants
CONST CARTPID = 0
CONST CARTPNAME = 1
CONST CARTPPRICE = 2
CONST CARTPQUANTITY = 3
' Get The Shopping Cart
IF NOT isArray( Session( "cart" ) ) THEN
DIM localCart( 4, 50 )
ELSE
localCart = Session( "cart" )
END IF
' Get Product Information
productID = TRIM( Request( "pid" ) )
productName = TRIM( Request( "productName" ) )
productPrice = TRIM( Request( "productPrice" ) )
IF Request( "shippingPrice" ) <> "" THEN
shippingPrice = TRIM( Request( "shippingPrice" ) )
Response.Write pid & " " & productName & " " & productPrice & " " & shippingPrice
IF shippingPrice = -1 THEN
IF productPrice = 25 THEN
shippingPrice = 9.5
productPrice = productPrice + shippingPrice
END IF
IF productPrice >= 30 and productPrice < 40 THEN
shippingPrice = 10
productPrice = shippingPrice + productPrice
END IF
IF productPrice >= 40 and productPrice < 49 THEN
shippingPrice = 10
productPrice = shippingPrice + productPrice
END IF
IF productPrice >= 50 and productPrice < 59 THEN
shippingPrice = 11.5
productPrice = shippingPrice + productPrice
END IF
IF productPrice >= 60 and productPrice < 69 THEN
shippingPrice = 12
productPrice = shippingPrice + productPrice
END IF
IF productPrice >= 70 and productPrice < 79 THEN
shippingPrice = 13
productPrice = shippingPrice + productPrice
END IF
ELSE
productPrice = shippingPrice + productPrice
END IF
Response.Write "<br>PRODUCT PRICE: " & productPrice
END IF
' Add Item to cart
IF productID <> "" THEN
foundIT = FALSE
FOR i = 0 TO UBOUND( localCart )
IF localCart( CARTPID, i ) = productID THEN
localCart( CARTPQUANTITY, i ) = localCart( CARTPQUANTITY, i ) + 1
foundIT = TRUE
EXIT FOR
END IF
NEXT
IF NOT foundIT THEN
FOR i = 0 TO UBOUND( localCart, 2 )
IF localCart( CARTPID, i ) = "" THEN
localCart( CARTPID, i ) = productID
localCart( CARTPNAME, i ) = productName
localCart( CARTPPRICE, i ) = productPrice
localCart( CARTPQUANTITY, i ) = 1
EXIT FOR
END IF
NEXT
END IF
END IF
' Update Shopping Cart Quantities
IF Request( "updateQ" ) <> "" THEN
FOR i = 0 TO UBOUND( localCart, 2 )
newQ = TRIM( Request( "pq"&localCart( CARTPID, i ) ) )
deleteProduct = TRIM( Request( "pd"&localCart( CARTPID, i ) ) )
IF newQ = "" or newQ = "0" or deleteProduct <> "" THEN
localCart( CARTPID, i ) = ""
ELSE
IF isNumeric( newQ ) THEN
localCart( CARTPQUANTITY, i ) = newQ
END IF
END IF
NEXT
END IF
' Update Session variable with Array
Session( "cart" ) = localCart
%>
<%
orderTotal = 0
%>
<form method="post" action="cart.asp">
<input name="updateQ" type="hidden" value="1">
<table border=0 cellpadding=4 cellspacing=0 width=700>
<tr>
<td class="itemTitle">Product</td>
<td class="itemTitle">Price</td>
<td class="itemTitle">Quantity</td>
</tr>
<%
FOR i = 0 TO UBOUND( localCart, 2 )
IF localCart( CARTPID, i ) <> "" THEN
orderTotal = orderTotal + ( localCart( CARTPPRICE, i ) * localCart( CARTPQUANTITY, i ) )
%>
<tr>
<td class="bodyText">
<%=Server.HTMLEncode( localCart( CARTPNAME, i ) )%>
</td>
<td class="bodyText">
<%=formatCurrency( localCart( CARTPPRICE, i ) )%>
</td>
<td>
<input name="pq<%=localCart( CARTPID, i )%>" type="text" size=4
value="<%=localCart( CARTPQUANTITY, i )%>">
<input name="pd<%=localCart( CARTPID, i )%>" type="checkbox" value="1"><span class="bodyText">Delete</span>
</td>
</tr>
<%
END IF
NEXT
%>
<tr>
<td colspan=2 align=right class="itemTitle">
<b>Order Total:</b>
</td>
<td class="itemTitle">
<%=formatCurrency( orderTotal )%>
</td>
</tr>
<tr>
<td colspan=3>
<table border=0>
<tr>
<td align="center">
<input type="image" src="images/update_cart.gif" alt="Update Cart">
</td>
</form>
<form action="default.asp">
<td>
<input type="image" src="images/keep_shopping.gif" alt="Keep Shopping">
</td>
</form>
</tr><tr>
<td colspan=3 align="center">
<form method="post" action="checkout.asp">
<input type="image" src="images/checkout.gif" alt="Checkout">
</form>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td></tr>
</table>
</td>
</tr>
</table>
<table width="800" border="0" cellpadding="0" cellspacing="0" id="footer">
<tr>
<td><img src="images/footer_home.gif" border="0" usemap="#home_footer"><img src="images/footer_about.gif" border="0" usemap="#about"><img src="images/footer_ordering.gif" border="0" usemap="#ordering"><img src="images/footer_mailing.gif" border="0" usemap="#mailinglist"><img src="images/footer_contact.gif" border="0" usemap="#contact"></td>
</tr>
</table>
</body>
</html>
|

June 5th, 2004, 01:48 AM
|
Friend of Wrox
|
|
Join Date: May 2004
Posts: 642
Thanks: 0
Thanked 43 Times in 42 Posts
|
|
At the time of adding number, convert the numbers to int or double by using cint() or cdbl() functions.
Om Prakash
|

June 6th, 2004, 06:15 PM
|
Friend of Wrox
|
|
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
|
|
Remember, CInt function always rounds it to the nearest even number.
Wind is your friend
Matt
|

June 7th, 2004, 04:43 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 231
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
The CInt() function does NOT round to the nearest even number, it rounds to the nearest whole number (integer). For example CInt(1.49) = 1 and CInt(1.50) = 2.
Another method would be to use the Val() function, this converts the expression to a number (the return type is actually a double but this doesn't matter for whole numbers or even single's because they will be implicitly converted anyway), if it can't be converted then zero is returned. For example Val(1.49) = 1.49, Val(5) = 5 and Val("Hello World") = 0.
There are other functions that can be used to convert expressions to numbers. Here is a list of them and what they return:
CByte() Converts the expression to a Byte data type.
CInt() Converts the expression to an Integer data type.
CLng() Converts the expression to a Long data type.
CSng() Converts the expression to a Single data type.
CDbl() Converts the expression to a Double data type.
CCur() Converts the expression to a Currency data type.
Another function that you could use is the IsNumeric() function, this takes an expression and returns True if it can be converted to a number and False otherwise. This way you can check that valid data has been posted to your script and if it is not valid you could return an error, or redirect the user to another page.
Here is the code I would use when getting the product information:
Code:
If IsNumeric(Request("pid")) And _
Len(Trim(Request("productName"))) > 0 And _
IsNumeric(Request("productPrice")) And _
IsNumeric(Request("shippingPrice")) Then
productID = Val(Request("pid"))
productName = Trim(Request("productName"))
productPrice = Val(Request("productPrice"))
shippingPrice = Val(Request("shippingPrice"))
...
Else
Response.Write("An error has occurred")
End If
You could of course substitute the Val's for CInt's or CDbl's or whatever is appropriate for your script.
Regards
Owain Williams
|

June 7th, 2004, 06:56 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 231
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I have spotted another problem with your code, it is the section where you calculate the shipping price based on the product price. If the product price is less than 30 but not 25, or equal to 49 or 59 or 69 or is greater than 78 then no shipping price will be calculated and the product price will not be updated.
This is because your first test is 'IF productPrice = 25 THEN' well what about anything else that is less than 30 but not equal to 25 (for example 24!)? There may be a reason for this for example the only product you sell that is less than 30 my be 25, however it is not considered good coding practice to hard code things like this, if you change the price of your products but you forget to update this section of code then the shipping price may not be added to the product price (thus making you out of pocket).
The second test you perform is 'IF productPrice >= 30 and productPrice < 40 THEN' which will hold True for all products that are priced between 30 and 39, however the third test you perform is 'IF productPrice >= 40 and productPrice < 49 THEN' which will only hold True for prices between 40 and 48 but not 49. Again the reason for this may be that you don't sell any products for 49 (or 59 or 69), but as before this is bad coding practice. You have done the same thing on subsequent tests, either change the second part of the test (productPrice < 49) to <= 49 or < 50 (or what ever is appropriate for the test you are performing).
The last test you perform is 'IF productPrice >= 70 and productPrice < 79 THEN' again what about products that sell for more than 78? This may be another case of 'I don't sell anything for more than 78' however this is also bad programing practice. You should either add another test and shipping price band for anything above 78 or change the final test to only be 'IF productPrice >= 70 THEN'
Another point is that it is conceivably possible that you would change the shipping price bands, for example all products that sell for between 40 and 59 and not just 50 and 59 should have a shipping price of 11.5. To do this you may change the test 'IF productPrice >= 50 and productPrice < 59 THEN' to 'IF productPrice >= 40 and productPrice < 59 THEN' but forget to remove the previous test ('IF productPrice >= 40 and productPrice < 49 THEN'). In this example any product that is priced between 40 and 49 will hold true for both tests and 10 and 11.5 will be added to the product price and not just 10 or 11.5. To get round this you can either use EndIf's instead of ENDing the If statement each time, or you could use a Select Case statement.
Here is the code that I would have written to calculate the shipping price (and add it to the product price):
Code:
If shippingPrice < 0 Then
Select Case productPrice
Case Is < 30
shippingPrice = 9.5
Case 30 To 39
shippingPrice = 10
Case 40 To 49
shippingPrice = 10
Case 50 To 59
shippingPrice = 11.5
Case 60 To 69
shippingPrice = 12
Case Else
shippingPrice = 13
End Select
End If
productPrice = productPrice + shippingPrice
I hope this helps and my apologies for this being slightly off topic.
Regards
Owain Williams
|

June 7th, 2004, 06:40 PM
|
Friend of Wrox
|
|
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
|
|
Owain
Gidday mr BOLD, a quote:
----------start quote--------------
Note CInt differs from the Fix and Int functions, which truncate, rather than round, the fractional part of a number. When the fractional part is exactly 0.5, the CInt function always rounds it to the nearest even number. For example, 0.5 rounds to 0, and 1.5 rounds to 2.
----------finish quote--------------
Reference:mk:@MSITStore:C:\CHMS\VBS55.CHM::/HTML/vbscopy.htm
------------------------------------
I'm sure youll agree the above reference is the authority
Wind is your friend
Matt
|

June 9th, 2004, 03:17 PM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 231
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Matt
Gidday Mr. Nit-Picker, OK, so the CInt() function will round to the nearest whole number if the decimal (or fractional part) of it is exactly 0.5. Well you learn something new every day don't you :).
However I still stand by my post:
Quote:
quote: Originally posted by owain
The CInt() function does NOT round to the nearest even number, it rounds to the nearest whole number (integer).
|
Because as a general rule it does round to the nearest whole number, and it is only when the fractional part is exactly 0.5. You said in your post:
Quote:
quote: Originally posted by mat41
|
Quote:
Remember, CInt function always rounds it to the nearest even number.
|
Well it does not always round to the nearest even number, it only does this when the fractional part is exactly 0.5.
Regards
Owain Williams
|

June 9th, 2004, 06:04 PM
|
Friend of Wrox
|
|
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
|
|
Owain (aka <b>)
Nice injection - Since (in my experience) this is the best forum around with one of the most helpful and technicaly brilliant user bases, I thought my '06/07/2004 : 11:40:14 PM' post was neccessary for a factual reference to keep it that way. Yes it was a bit nit picky (how ever factual). But, I believe our posts read one after the other will assist somebody who knows very littke about cInt. Hey, they may even laugh a bit - I did
All faqs and no spice, naaa;
faqs sprinkled with a touch of spice, nice!
Regards
Wind is your friend
Matt
|

August 8th, 2004, 03:43 AM
|
Registered User
|
|
Join Date: Aug 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
what is ejb?
|

August 8th, 2004, 05:57 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 231
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Excuse me, what do you mean 'what is ejb?' How should I know what ejb is? What are you refering to. I have just quickly scanned the post and I can find no references to ejb.
Regards
Owain Williams
|
|
 |