Click to See Complete Forum and Search --> : how to get back variable from ASP to javascript


andrewchai
03-30-2005, 02:58 AM
how do i get back the variable balance in the ASP. like show below ??


<script language="javascript">
function calculate(){
alert("HI");
var yr_ent;
var day;
var balance;

day = document.getElementById("day");
yr_ent = document.getElementById("year_ent");
balance = yr_ent.value - day.value;

alert(balance);
document.write("<% response.cookies("balance") =" + balance + "%>" );

document.write("<% response.write(request.cookies("balance")) %>");


}
</script>

buntine
03-30-2005, 03:20 AM
ASP and JavaScript are not related in any way. You simply cannot use the two technologies in unison like this. You will have to send the value of the JavaScript variable balance to the server if you want to use it in an ASP environment.

In the context of this problem, the bottom-line is that the ASP code will be executed and forgotten about before the JavaScript variable even exists.

I suggest you read up on the concepts of server-side programming.

Regards.

lmf232s
03-30-2005, 04:35 PM
NEVER MIND:
I just reread your post and you want to take a javascript variable and pass it to ASP not the other way around. Ya you cant do that.

What do you want to do?
Because once your done with the calculation you could set a textbox to the value of balance or anything else, then once you submit the form you have the new balance value.



If you are calling this sub you can set your asp value where you are going to call the javascript and it will work.
So you could do this

<input type=button name=cmdCalc value="Calculate(<%=balance%>);">

<script language="javascript">
function calculate(balance){
alert("HI");
var yr_ent;
var day;
var balance;

day = document.getElementById("day");
yr_ent = document.getElementById("year_ent");
balance = yr_ent.value - day.value;

alert(balance);
document.write("<% response.cookies("balance") =" + balance + "%>" );
document.write("<% response.write(request.cookies("balance")) %>");
}
</script>

phpnovice
03-30-2005, 06:03 PM
how do i get back the variable balance in the ASP. like show below ??
If you don't need a response from the ASP code, then you can cheat and easily pass such JavaScript variables to ASP as follows:

var passToASP = new Image();
passToASP.src = "somepage.asp?balance="+balance;

There is also a legitimate way to do this from JavaScript, but it is harder. If you want to know more about the harder method, start with the information in this post:

http://www.webdeveloper.com/forum/showpost.php?p=342409&postcount=3

buntine
03-30-2005, 06:41 PM
var passToASP = new Image();
passToASP.src = "somepage.asp?balance="+balance;

lol. What a hack. :D

phpnovice
03-30-2005, 07:05 PM
lol. What a hack. :D
That's why I called it a cheat. :p But it works -- and very well.

I once had some fun experimenting with this. I called this page "client_side.asp":

<% @Language=VBScript %>
<% Option Explicit %>
<html><head><title>Client-side HTML page</title></head>
<body>
<%
Dim nm
Response.Write "<p>Start of session variables:<br>" & vbCrLf
For Each nm in Session.Contents
Response.Write nm & " = " & Session.Contents(nm) & "<br>" & vbCrLf
Next
Response.Write "End of session variables.</p>" & vbCrLf
%>
<form onSubmit="return false;">
<p><input type="button" value="Set Time Stamp" onclick="
var temp = new Image();
temp.src = 'server_side.asp?time_stamp='+escape((new Date()).toLocaleString());
window.setTimeout('self.location.reload(true)',500);
return true;"></p>
<p><input type="button" value="Delete Session" onclick="
var temp = new Image();
temp.src = 'server_side.asp?delete=yes&force='+((new Date()).getTime());
window.setTimeout('self.location.reload(true)',500);
return true;"></p>
</form>
</body>
</html>

and I called this page "server_side.asp":

<% @Language=VBScript %>
<% Option Explicit %>
<%
Dim nm
If Request.QueryString("delete") <> "yes" Then
For Each nm in Request.QueryString
Session(nm) = Request.QueryString(nm)
Next
Else
Session.Abandon
End If
%>

Try it out.

andrewchai
03-30-2005, 10:11 PM
Thanks to everyone who give guideline to me !