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?

1

3 Answers 3

0

Modify your code as below

<%
  int i=1; 
%>
<form name="ifrm" onsubmit="return validateform('<%=i%>')" action="Givetest" method="post"> 
  //code
</form>
Sign up to request clarification or add additional context in comments.

4 Comments

Error:cannot find variable i
@saumyarajsinh try <% int i=1; %> before <form> tag
@saumyarajsinh replace <%> with %> after intializing i variable and place it before form tag, i think it works perfect :)
yes,it worked but please have a look at my code.I edited the question.The variable "t" is not able to get a value inside the try block. Thank you
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>

2 Comments

Error:cannot find variable i
Where ? JSP or Javascript ?
0

try this.code in function

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


onclick="<%="javascript:validateform("+i+")"%>"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.