Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Apr 2005
    Location
    Chicago
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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!!

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    New to the CF scene
    Join Date
    Apr 2005
    Location
    Chicago
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,032
    Thanks
    0
    Thanked 248 Times in 245 Posts
    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.

  • #5
    New to the CF scene
    Join Date
    Apr 2005
    Location
    Chicago
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,032
    Thanks
    0
    Thanked 248 Times in 245 Posts
    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.

  • #7
    New to the CF scene
    Join Date
    Apr 2005
    Location
    Chicago
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.


  •  

    Posting Permissions

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