After processing values with a JS it returns NaN

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • GeryLuz
    New Member
    • Jul 2007
    • 4

    After processing values with a JS it returns NaN

    Hi ! I need someone to help me with this.

    I have an ASP page with a form that sends two values coming from a textbox to other. What i need is: A second page must receive them and pass them as parameters of a .JS function, who performs a sum and returns the result to it, which shows the value in an alert message.
    I don't really understand how to manage a Js inside VBScript or how is the best way to do it, this code is the only way i could solve it.
    I test my Js passing fixed numeric values and works fine, but as it is requested in my code, the alert message shows "NaN"
    I'll appreciate your suggestions! Thks!!

    ---------------------------------------------
    ASP PAGE WHICH RECEIVES THE VALUES
    ---------------------------------------------

    <SCRIPT>
    var primero
    </SCRIPT>

    <SCRIPT>
    var segundo
    </SCRIPT>

    <%@LANGUAGE="Ja vaScript"%>

    <%
    primero = new String(Request. Form("uno"))
    segundo = new String(Request. Form("dos"))
    %>

    <html>

    <head>

    <script src="sumamos.js " language="javas cript">
    </script>

    </head>

    <body>

    <script language="javaS cript">

    var total

    total = suma (primero, segundo)

    alert(total)

    </script>

    </body>
    </html>

    ------------------
    EXTERNAL JS
    -------------------

    function suma (uno, dos)
    {

    suma = (eval(uno) + eval(dos))

    return suma

    }
  • iam_clint
    Recognized Expert Top Contributor
    • Jul 2006
    • 1207

    #2
    parseFloat because you can't add two strings togethor

    Example
    "1" + "2" = NaN
    but
    1 + 2 = 3

    so parseFloat("1") = 1 parseFloat("2") ; = 2;
    [CODE=javascript]
    function suma (uno, dos) {
    uno = parseFloat(uno) ;
    dos = parseFloat(dos) ;
    suma = Math.floor(uno+ dos);
    return suma
    }
    [/CODE]

    Comment

    • AdrianH
      Recognized Expert Top Contributor
      • Feb 2007
      • 1251

      #3
      Originally posted by GeryLuz
      Hi ! I need someone to help me with this.

      I have an ASP page with a form that sends two values coming from a textbox to other. What i need is: A second page must receive them and pass them as parameters of a .JS function, who performs a sum and returns the result to it, which shows the value in an alert message.
      I don't really understand how to manage a Js inside VBScript or how is the best way to do it, this code is the only way i could solve it.
      I test my Js passing fixed numeric values and works fine, but as it is requested in my code, the alert message shows "NaN"
      I'll appreciate your suggestions! Thks!!

      ---------------------------------------------
      ASP PAGE WHICH RECEIVES THE VALUES
      ---------------------------------------------

      [code=asp]<SCRIPT>
      var primero
      </SCRIPT>

      <SCRIPT>
      var segundo
      </SCRIPT>

      <%@LANGUAGE="Ja vaScript"%>

      <%
      primero = new String(Request. Form("uno"))
      segundo = new String(Request. Form("dos"))
      %>

      <html>
      <head>
      <script src="sumamos.js " language="javas cript">
      </script>
      </head>
      <body>
      <script language="javaS cript">
      var total
      total = suma (primero, segundo)
      alert(total)
      </script>
      </body>
      </html>[/code]

      ------------------
      EXTERNAL JS
      -------------------

      [code=JavaScript]function suma (uno, dos)
      {
      suma = (eval(uno) + eval(dos))
      return suma
      }[/code]
      Please indent, it makes it easier to read and understand what you are doing.

      As for your question, change
      [code=javascript] total = suma (primero, segundo)[/code]
      to
      [code=javascript] total = suma (<% echo primero %>, <% echo segundo %>)[/code]

      Please forgive me if I've not used the correct command to echo the values. I've not done ASP in a long while.

      Basicly, javascript is executed on the browser side, so the browser must see the values. By echoing them from the asp page, that will be sent to the browser for evaluation.

      BTW, you don't need to eval the paramter variables. This:
      [code=javascript] suma = (eval(uno) + eval(dos))[/code]
      can be replaced with this:
      [code=javascript] suma = uno + dos;[/code]

      Also, I'm not positive, but I don't think you should assign the sum to the name of the function. IIRC, that is a VBism. JavaScript is closer to C/C++/Java. What you return is what is after the return statement. So you can change your function to this:

      [code=JavaScript]function suma (uno, dos)
      {
      return uno + dos;
      }[/code]

      Assigning the name suma inside the function may screw up further calls to suma since function and variables share the same namespace.

      Hope this helps.


      Adrian

      Comment

      • iam_clint
        Recognized Expert Top Contributor
        • Jul 2006
        • 1207

        #4
        its
        [CODE=javascript]total = suma (<%=primero%>, <%=segundo%>)[/CODE]

        and you will still want to parseFloat.


        and use the Math function.

        Comment

        • AdrianH
          Recognized Expert Top Contributor
          • Feb 2007
          • 1251

          #5
          Instead of using Math.floor(), and parseFloat(), you can use parseInt(var, radix); where var is the variable you are converting and radix is probably 10 in your case.

          BTW, why do you want to pass it on to javascript to sum the values? Why not do the summation on the ASP side and then get the asp page to write out:
          [code=asp]
          <%
          // You may have to do some clean up here to make sure the values are numbers
          sum = Request.Form("u no") + Request.Form("d os")
          %>
          <html>
          <head>
          <script language="javas cript">alert("< %= sum %>");</script>
          </head>
          <body>
          </body>
          </html>
          [/code]

          Personally, I write web pages without javascript and then add it in afterwards so I can see what it will look like without it. Doing it the other way will make it more difficult to create a nice non-javascript enabled web page. I usually shut it off on sites I do not know.


          Adrian

          Comment

          Working...