0

I have a variable in my jsp page which i want to pass as an argument to a javascript function.How to do it?

code:-

 <%
       String ts=request.getParameter("testname");
       session.setAttribute("tname", ts);
  Connection con=null;
  Statement s=null;
  ResultSet r=null;
  int t=60000;
  try
        {
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      con=DriverManager.getConnection("jdbc:odbc:online_testing");
      s=con.createStatement();  
      r=s.executeQuery("select * from "+ts+"");   
String time=r.getString("duration");
t=Integer.parseInt(time)*60000;     

  }
        catch(Exception e1)
        {
        response.setContentType("text/html");
        out.println(e1.toString());
        }
 %>
    <body onload="setTimeout('submitForm()',<%=t%>)">

It is taking only the initial value of t,not the updated one. How can I pass the value of variable "t" in function setTimeout() as an argument?

sam
  • 43
  • 2
  • 9
  • possible duplicate of [Javascript/JSP: How to pass JSP variables value on one page to javascript variables on next page?](http://stackoverflow.com/questions/3559058/javascript-jsp-how-to-pass-jsp-variables-value-on-one-page-to-javascript-variab) – Qantas 94 Heavy Oct 23 '13 at 12:05

3 Answers3

0

try this.code in function

"javascript:validateform('<%=i%>')"


onclick="<%="javascript:validateform("+i+")"%>"
suresh manda
  • 659
  • 1
  • 8
  • 25
0

Change order of initialization of i, then you can use it

<%
                int i=1; //1 is just for e.g ,getting this value from database in project
%>
<form name="ifrm" onsubmit="return validateform(<%=i%>)" action="Givetest" method="post"> 
<input type="radio" name="r<%=i%>" value="<%=b%>"><label><%=b%></label><br>
</form>
MC ND
  • 69,615
  • 8
  • 84
  • 126
0

Modify your code as below

<%
  int i=1; 
%>
<form name="ifrm" onsubmit="return validateform('<%=i%>')" action="Givetest" method="post"> 
  //code
</form>
Naveen Kumar Alone
  • 7,536
  • 5
  • 36
  • 57