473,385 Members | 1,483 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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="JavaScript"%>

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

<html>

<head>

<script src="sumamos.js" language="javascript">
</script>

</head>

<body>

<script language="javaScript">

var total

total = suma (primero, segundo)

alert(total)

</script>

</body>
</html>

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

function suma (uno, dos)
{

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

return suma

}
Jul 13 '07 #1
4 1888
iam_clint
1,208 Expert 1GB
parseFloat because you can't add two strings togethor

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

so parseFloat("1") = 1 parseFloat("2"); = 2;
Expand|Select|Wrap|Line Numbers
  1. function suma (uno, dos) {
  2. uno = parseFloat(uno);
  3. dos = parseFloat(dos);
  4. suma = Math.floor(uno+dos);
  5. return suma
  6. }
  7.  
Jul 13 '07 #2
AdrianH
1,251 Expert 1GB
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
---------------------------------------------

Expand|Select|Wrap|Line Numbers
  1. <SCRIPT> 
  2.   var primero
  3. </SCRIPT> 
  4.  
  5. <SCRIPT> 
  6.   var segundo
  7. </SCRIPT> 
  8.  
  9. <%@LANGUAGE="JavaScript"%>
  10.  
  11. <%
  12.   primero = new String(Request.Form("uno"))
  13.   segundo = new String(Request.Form("dos"))
  14. %>
  15.  
  16. <html>
  17.   <head>
  18.     <script src="sumamos.js" language="javascript">
  19.     </script>
  20.   </head>
  21.   <body>
  22.     <script language="javaScript">
  23.       var total
  24.       total = suma (primero, segundo)
  25.       alert(total)
  26.     </script>
  27.   </body>
  28. </html>
------------------
EXTERNAL JS
-------------------

Expand|Select|Wrap|Line Numbers
  1. function suma (uno, dos)
  2. {
  3.   suma = (eval(uno) + eval(dos))
  4.   return suma
  5. }
Please indent, it makes it easier to read and understand what you are doing.

As for your question, change
Expand|Select|Wrap|Line Numbers
  1.       total = suma (primero, segundo)
to
Expand|Select|Wrap|Line Numbers
  1.       total = suma (<% echo primero %>, <% echo segundo %>)
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:
Expand|Select|Wrap|Line Numbers
  1.   suma = (eval(uno) + eval(dos))
can be replaced with this:
Expand|Select|Wrap|Line Numbers
  1.   suma = uno + dos;
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:

Expand|Select|Wrap|Line Numbers
  1. function suma (uno, dos)
  2. {
  3.   return uno + dos;
  4. }
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
Jul 13 '07 #3
iam_clint
1,208 Expert 1GB
its
Expand|Select|Wrap|Line Numbers
  1. total = suma (<%=primero%>, <%=segundo%>)
and you will still want to parseFloat.


and use the Math function.
Jul 13 '07 #4
AdrianH
1,251 Expert 1GB
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:
Expand|Select|Wrap|Line Numbers
  1. <% 
  2.   // You may have to do some clean up here to make sure the values are numbers
  3.   sum = Request.Form("uno") + Request.Form("dos")
  4.  %>
  5. <html>
  6.   <head>
  7.     <script language="javascript">alert("<%= sum %>");</script>
  8.   </head>
  9.   <body>
  10.   </body>
  11. </html>
  12.  
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
Jul 13 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: dmcconkey | last post by:
Hi folks, I have a client with four websites. Each site has a contact form that is identical. They all have "required" fields validated through a JavaScript onSubmit() function. Upon validation,...
17
by: MLH | last post by:
A97 Topic: If there is a way to preserve the values assigned to global variables when an untrapped runtime error occurs? I don't think there is, but I thought I'd ask. During development, I'm...
2
by: Jackal | last post by:
I have a form page(abc.aspx) that contains some fields need to be filled in before submitting, and the action of the form is "def.aspx". After peocessing the filled-in data, it'll return the...
5
by: Steve - DND | last post by:
If I declare a property as ThreadStatic, or I use the thread Context to set values on, will they be retained after a page processes? When a page finishes, is all data on the thread wiped before it...
4
by: Siang Hwee | last post by:
Hi. I am facing some problem in Asp.net. I am developing a payroll application currently. This is a web-based application. User need to click on "Process" button in order to process the payroll...
2
by: thomasamillergoogle | last post by:
I would like to have a web service continue a little bit of processsing after it returns the data to the consumer. What is the proper way to do this? Please provide an bit of example code on how to...
11
by: Zorro | last post by:
Hello guys, <SELECT name = "f_hello"> <OPTION value = 'H'>Hello</OPTION>" <OPTION value = 'G'>Good-Bye</OPTION>" </SELECT> In the case above, I can easily retrieve the f_hello variable ...
4
by: News | last post by:
Hi Everyone, The attached code creates client connections to websphere queue managers and then processes an inquiry against them. The program functions when it gets options from the command...
0
by: sethuraman | last post by:
Hi, I have had a problem before, I am not sure about the exact exception that is thrown.. but I am sure that there is an exception thrown after processing 600 and odd rows in a DB2 JDBC...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.