The_Gutsy
asked on
Passing JavaScript values to JSP variables
Can any body correct the follwing code
<Script language="JavaScript">
function test( x )
{
<%
int num = x;
num = num * 2;
%>
v.value = "<%out.print(num);%>";
}
<input type="button" name="b" value="test" onClick="test(5)">
<input type="text" name="v" value="0">
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----
In short, I am trying to pass JavaScript value to JSP variable. I hope that it is possible to do that. If it is possible then how can I do it. I want to assing the variable x passed to the JavaScript function called test to the JSP variable called num.
Regards,
Ageel
<Script language="JavaScript">
function test( x )
{
<%
int num = x;
num = num * 2;
%>
v.value = "<%out.print(num);%>";
}
<input type="button" name="b" value="test" onClick="test(5)">
<input type="text" name="v" value="0">
--------------------------
In short, I am trying to pass JavaScript value to JSP variable. I hope that it is possible to do that. If it is possible then how can I do it. I want to assing the variable x passed to the JavaScript function called test to the JSP variable called num.
Regards,
Ageel
ASKER
I have given you this simple code just as an example
but actually I need to pass the variables from javascript to jsp because I need to do so
Actually, I am using these parameters or let's say variables to get some information from the database. if java script can connect to the database that will be fine but I can't connect to the database using javascript without JSP
hmmmm
I get big headache, I don't knwo what I am supposed to do so that I solve this problem
but actually I need to pass the variables from javascript to jsp because I need to do so
Actually, I am using these parameters or let's say variables to get some information from the database. if java script can connect to the database that will be fine but I can't connect to the database using javascript without JSP
hmmmm
I get big headache, I don't knwo what I am supposed to do so that I solve this problem
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Actually, this looks like a good resource showing cool ways of doing what you want to do:
http://www-106.ibm.com/developerworks/web/library/wa-exrel/
http://www-106.ibm.com/developerworks/web/library/wa-exrel/
you can assign java variable value to a javascript variable. since u need to get some information from db and use it in your javascript, first retrieve the data you want from db using java, then u can assign value to the javascript function.
For example:
<%
String num = DBUtil.getDBValue("loop");
%>
<html>
<head>
<Script language="JavaScript">
function test( x )
{
var num1 = parseInt('<%=num%>');
num1 = num1*2*x;
v.value = num1;
}
</script>
</head>
<body>
<form>
<input type="button" name="b" value="test" onClick="test(5)">
<input type="text" name="v" value="0">
</form>
</body>
</html>
For example:
<%
String num = DBUtil.getDBValue("loop");
%>
<html>
<head>
<Script language="JavaScript">
function test( x )
{
var num1 = parseInt('<%=num%>');
num1 = num1*2*x;
v.value = num1;
}
</script>
</head>
<body>
<form>
<input type="button" name="b" value="test" onClick="test(5)">
<input type="text" name="v" value="0">
</form>
</body>
</html>
i agree still you can
pass from URL to the self page an then getParamter for example
jafa script
reload.jsp?Num='form.num1. value'
and get jsp
request.getParameter("Num" );
jeje its mi idea
pass from URL to the self page an then getParamter for example
jafa script
reload.jsp?Num='form.num1.
and get jsp
request.getParameter("Num"
jeje its mi idea
That's because, it is only by form posting that client based javascript code can talk to server based JSP code...
<Script language="JavaScript">
function test( x )
{
v.value = x * 2 ;
}
</script>
<input type="button" name="b" value="test" onClick="test(5)">
<input type="text" name="v" value="0">
You know you can do that I guess (and this was just an example) ;-)
You will have to build up whatever data you need into the HTML page dynamically using JSP, so that Javascript can use that data to do whatever processing you wanted to do on the server...
Tim