Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-14-2005, 02:50 PM   PM User | #1
camels43
New to the CF scene

 
Join Date: Apr 2005
Location: Chicago
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
camels43 is an unknown quantity at this point
Unhappy Javascript not transferring correct value if the value is numeric

The problem is that on a webpage.asp, the items size with corresponding price does not transfer correctly to the shopping cart if the value of the size is a numeric value. It works fine if the size is a text value.

Let's take ItemA for example:
Item A comes in small for $2 and medium for $4.
I choose the radio button for the medium item and click "add to cart".
The cart reads as follows (which is correct):
ItemA: ItemA:medium @ 4
Price: $4


However, Lets take ItemB for example:
Item B comes in sizes 16" for $2 and 18" for $4.
I choose the radio button for the 18" item and click "add to cart".
The cart reads as follows (which is not correct):
ItemB: ItemB:18
Price: $2


The code that I think pertains to this issue is posted below:
The first section is the javascript code and the second section is the excerpt from the form.

function ReadForm (obj1, tst) { // process radio and checkbox
var i,amt,des,obj,pos,val;
amt = obj1.baseamt.value*1.0; // base amount
des = obj1.basedes.value; // base description
for (i=0; i<obj1.length; i++) { // run entire form
obj = obj1.elements[i]; // a form element
if (obj.type == "checkbox" || // checkboxes
obj.type == "radio") { // and radios
if (obj.checked) { // did user check it?
val = obj.value; // the value of the selection
pos = val.indexOf ("@"); // price set?
if (pos >= 0) amt = val.substring (pos + 1)*1.0;
pos = val.indexOf ("+"); // price increment?
if (pos >= 0) amt = amt + val.substring (pos + 1)*1.0;
pos = val.indexOf ("%"); // percent change?
if (pos >= 0) amt = amt + (amt * val.substring (pos + 1)/100.0);
if (des.length == 0) des = val;
else des = des + ", " + val; // accumulate value
}


<input name="Size" type="radio" id="Size"
onclick="ReadForm (this.form, false);"
value="Size <%=(rsSelectItem.Fields.Item("Prod_Size").Value)%> @ <%=(rsSelectItem.Fields.Item("Prod_Price").Value)%>


Please, if someone can help me out it would be greatly appreciated!!
Thank you!!
camels43 is offline   Reply With Quote
Old 04-14-2005, 03:55 PM   PM User | #2
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
all the element's values in javascript are strings, not numbers, so that you better transform them into numbers before any math operation.
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 04-14-2005, 05:36 PM   PM User | #3
camels43
New to the CF scene

 
Join Date: Apr 2005
Location: Chicago
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
camels43 is an unknown quantity at this point
I am just learning javascript so bear with me here but...

My goal is not to do a math function with the size value though? I just want it to look at the size value as a string, even though some of the size values are numbers... I just want it to pick up the value of the size whether it is a text or numeric value and transfer the size and corresponding information for that size to the shopping cart...

Does that make sense?? if not, I will elaborate

Last edited by camels43; 04-15-2005 at 03:23 AM..
camels43 is offline   Reply With Quote
Old 04-15-2005, 09:08 AM   PM User | #4
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
Quote:
Originally Posted by camels43
<input name="Size" type="radio" id="Size"
onclick="ReadForm (this.form, false);"
value="Size <%=(rsSelectItem.Fields.Item("Prod_Size").Value)%> @ <%=(rsSelectItem.Fields.Item("Prod_Price").Value)%>
Please post sample generated values for that Size field and its corresponding correct cart display when "add to cart" is clicked.
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Old 04-15-2005, 01:51 PM   PM User | #5
camels43
New to the CF scene

 
Join Date: Apr 2005
Location: Chicago
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
camels43 is an unknown quantity at this point
Glenn,
Thank you for responding. Is this the info you want?

<<<<<<<
Let's take ItemA for example:
Item A comes in small for $2 and medium for $4.
I choose the radio button for the medium item and click "add to cart".
The cart reads as follows (which is correct):
ItemA: ItemA:medium @ 4
Price: $4

However, Lets take ItemB for example:
Item B comes in sizes 16" for $2 and 18" for $4.
I choose the radio button for the 18" item and click "add to cart".
The cart reads as follows (which is not correct):
[B]ItemB: ItemB:18
[B]Price: $2
>>>>>>

The correct cart display for ItemB should be:
ItemB: ItemB:18" @ 4
Price: $4

Last edited by camels43; 04-17-2005 at 04:36 PM..
camels43 is offline   Reply With Quote
Old 04-18-2005, 05:11 AM   PM User | #6
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
The radio button's value has a double quote causing the value to be truncated.

<input name="Size" type="radio" id="Size"
onclick="ReadForm (this.form, false);"
value="Size 18" @ 4">

Since the value is generated by ASP, you need to replace all occurrences of double quotes with &quot; by using HTMLEncode method.

value="Size <%=Server.HTMLEncode(rsSelectItem.Fields.Item("Prod_Size").Value)%> @ <%=(rsSelectItem.Fields.Item("Prod_Price").Value)%>"

Actually, you should HTML-encode all ASP variables that you embed inside HTML tags and attributes to avoid such kind of errors.
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Old 04-18-2005, 04:03 PM   PM User | #7
camels43
New to the CF scene

 
Join Date: Apr 2005
Location: Chicago
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
camels43 is an unknown quantity at this point
Thank You!!

Thank you so much!
It's always something simple eh?

That did the trick

And Glenn... sorry for the PM I'm brand new here... now I understand!!

Last edited by camels43; 04-18-2005 at 04:07 PM..
camels43 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:44 AM.


Advertisement
Log in to turn off these ads.