...

View Full Version : Javascript not transferring correct value if the value is numeric



camels43
04-14-2005, 03:50 PM
:confused: 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!! :o

Kor
04-14-2005, 04:55 PM
all the element's values in javascript are strings, not numbers, so that you better transform them into numbers before any math operation.

camels43
04-14-2005, 06:36 PM
:rolleyes: 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 :o

glenngv
04-15-2005, 10:08 AM
<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.

camels43
04-15-2005, 02:51 PM
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

glenngv
04-18-2005, 06:11 AM
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.

camels43
04-18-2005, 05:03 PM
Thank you so much!
It's always something simple eh?

That did the trick :thumbsup:

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum