By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,297 Members | 1,390 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,297 IT Pros & Developers. It's quick & easy.

problem in passing value from jsp to javascript.

P: 5
The following are the program i did......
and the problem im getting is object expected in onchange function at line 27(where i marked that one in bold for ur ease of identifying the error occuring area) in Task123.jsp. The value in not passed from Task123.jsp to Task113.js.


First Page : Task1.html
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script type="text/javascript" src="Task11.js">
  4. </script>
  5. </head>
  6.  
  7. <body>
  8.  
  9. <form name="myform" method="post" action="">
  10.  
  11. Select a Program:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  12. <select name="text1" style="width: 100px" onchange="showCustomer(document.myform.text1.value)">
  13. <option value="unbounded">unbounded</option>
  14. <option value="Program 1">Program 1</option>
  15. <option value="Program 2">Program 2</option>
  16. </select>
  17.  
  18. </form>
  19.  
  20. <div id="txtHint"><b>Program info will be listed here.</b></div>
  21.  
  22. </body>
  23. </html>
  24.  

second page : Task11.js

Expand|Select|Wrap|Line Numbers
  1. var xmlhttp
  2.  
  3. function showCustomer(str)
  4. {
  5. xmlhttp=GetXmlHttpObject();
  6. if (xmlhttp==null)
  7.   {
  8.   alert ("Your browser does not support AJAX!");
  9.   return;
  10.   }
  11. var url="Task123.jsp";
  12. url=url+"?q="+str;
  13. url=url+"&sid="+Math.random();
  14. xmlhttp.onreadystatechange=stateChanged;
  15. xmlhttp.open("GET",url,true);
  16. xmlhttp.send(null);
  17. }
  18.  
  19. function stateChanged()
  20. {
  21. if (xmlhttp.readyState==4)
  22.   {
  23.   document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
  24.   }
  25. }
  26.  
  27. function GetXmlHttpObject()
  28. {
  29. if (window.XMLHttpRequest)
  30.   {
  31.   // code for IE7+, Firefox, Chrome, Opera, Safari
  32.   return new XMLHttpRequest();
  33.   }
  34. if (window.ActiveXObject)
  35.   {
  36.   // code for IE6, IE5
  37.   return new ActiveXObject("Microsoft.XMLHTTP");
  38.   }
  39. return null;
  40. }
  41.  

Third page : Task123.jsp

Expand|Select|Wrap|Line Numbers
  1. <%@ page import="java.sql.*" %>
  2. <%
  3. String connectionURL = "jdbc:mysql://localhost/new";
  4. Connection connection = null;
  5. Statement statement;
  6. ResultSet rs;
  7. %>
  8.  
  9. <html>
  10. <head>
  11. <script type="text/javascript" src="Task113.js">
  12. </script>
  13. </head>
  14. <body>
  15.  
  16. <%
  17. String s=request.getParameter("q");
  18. Class.forName("com.mysql.jdbc.Driver");
  19. connection = DriverManager.getConnection(connectionURL, "root", "cng");
  20. statement = connection.createStatement();
  21. rs = statement.executeQuery("SELECT prid FROM project where pid='"+s+"'");
  22. String targetId=request.getParameter("q");
  23. %>
  24.  
  25. <form name="myform1" method="post" action="Task113.jsp">
  26. Projects for Program are :&nbsp;&nbsp;
  27. <select name="project" id="project" style="width: 100px" onChange="showCustomer6(document.myform1.project.value)">
  28.  
  29. <%
  30. while(rs.next())
  31. {
  32. String result=rs.getString(1);
  33. String home="unbounded";
  34. %>
  35.     <%
  36.     out.println(s+" consist of :<br>");
  37.     if(targetId.equalsIgnoreCase("unbounded"))
  38.     {%>
  39.     <option value="<%= home %>">
  40.     <% out.println(home); %>
  41.     </option>
  42.     <%}
  43.     else if(targetId.equalsIgnoreCase("Program 1"))
  44.     {%>
  45.     <option value="<%= result %>">
  46.     <%  out.println(result); %>
  47.     </option>
  48.     <%}
  49.     else
  50.     {%>
  51.     <option value="<%= result %>">
  52.     <%    out.println(result); %>
  53.     </option>
  54.     <%}
  55. }
  56.     %>
  57. </select>
  58.  
  59. </form>
  60. </body></html>
  61.  
Forth page : Task113.js

Expand|Select|Wrap|Line Numbers
  1. var xmlhttp
  2.  
  3. function showCustomer6(str)
  4. {
  5.     alert("1");
  6. xmlhttp=GetXmlHttpObject();
  7. if (xmlhttp==null)
  8.   {
  9.   alert ("Your browser does not support AJAX!");
  10.   return;
  11.   }
  12. var url="dummy.jsp";
  13. url=url+"?q1="+str;
  14. url=url+"&sid="+Math.random();
  15. xmlhttp.onreadystatechange=stateChanged;
  16. xmlhttp.open("GET",url,true);
  17. xmlhttp.send(null);
  18. }
  19.  
  20. function stateChanged()
  21. {
  22. if (xmlhttp.readyState==4)
  23.   {
  24.   document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
  25.   }
  26. }
  27.  
  28. function GetXmlHttpObject()
  29. {
  30. if (window.XMLHttpRequest)
  31.   {
  32.   // code for IE7+, Firefox, Chrome, Opera, Safari
  33.   return new XMLHttpRequest();
  34.   }
  35. if (window.ActiveXObject)
  36.   {
  37.   // code for IE6, IE5
  38.   return new ActiveXObject("Microsoft.XMLHTTP");
  39.   }
  40. return null;
  41. }
  42.  
  43.  
Fifth and the Final page : dummy.jsp

Expand|Select|Wrap|Line Numbers
  1. <%@ page import="java.sql.*" %>
  2. <%
  3. String connectionURL = "jdbc:mysql://localhost/new";
  4. Connection connection = null;
  5. Statement statement1 = null;
  6. ResultSet rs1 = null;
  7. %>
  8.  
  9. <html><body>
  10.  
  11. <%
  12.     String s1=request.getParameter("q1");
  13.     out.println("String is "+s1);
  14. Class.forName("com.mysql.jdbc.Driver");
  15. connection = DriverManager.getConnection(connectionURL, "root", "cng");
  16. statement1 = connection.createStatement();
  17. rs1 = statement.executeQuery("SELECT * FROM task1 where prid='"+s1+"'");
  18. %>
  19. <table border=1>
  20. <%while(rs1.next()) {%>
  21. <tr><td>
  22. <%
  23.     out.println(rs1.getString("tid")+"<br>");
  24.     %>
  25. </td>
  26. </tr><tr><td>
  27. <%
  28.     out.println(rs1.getString("tname")+"<br>");
  29.     %>
  30. </td>
  31. </tr><tr><td>
  32. <%
  33.     out.println(rs1.getString("prid")+"<br>");
  34.     %>
  35. </td>
  36. </tr><%
  37. }
  38. %></table><%
  39.  
  40. rs1.close();
  41.  
  42. %>
  43.  
  44. </body></html>.
  45.  
eagerly waiting for ur quick reply.thanks in advance.
Feb 26 '10 #1
Share this Question
Share on Google+
3 Replies


gits
Expert Mod 5K+
P: 5,390
could you please post the html-source that is rendered at the time where you expect that value? just load the page and go to view source in the browser ... then try to identify the relevant sections especially where the myform1 form is located so that we could have a closer look at it ...

kind regards
Mar 2 '10 #2

P: 5
sorry i caunt get you..........pls be elaborate............
Mar 2 '10 #3

gits
Expert Mod 5K+
P: 5,390
please post the page's source code view when the page is rendered in the borwser ... i think nobody wants to check that bunch of underlying code that will produce an output where the error could easyly spotted ... so please just post that relevant output here ...
Mar 2 '10 #4

Post your reply

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