problem in passing value from jsp to javascript.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ganihskmca
    New Member
    • Feb 2010
    • 5

    problem in passing value from jsp to javascript.

    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
    Code:
    <html>
    <head>
    <script type="text/javascript" src="Task11.js">
    </script>
    </head>
    
    <body>
    
    <form name="myform" method="post" action="">
    
    Select a Program:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <select name="text1" style="width: 100px" onchange="showCustomer(document.myform.text1.value)">
    <option value="unbounded">unbounded</option>
    <option value="Program 1">Program 1</option>
    <option value="Program 2">Program 2</option>
    </select>
    
    </form>
    
    <div id="txtHint"><b>Program info will be listed here.</b></div>
    
    </body>
    </html>

    second page : Task11.js

    Code:
    var xmlhttp
    
    function showCustomer(str)
    {
    xmlhttp=GetXmlHttpObject();
    if (xmlhttp==null)
      {
      alert ("Your browser does not support AJAX!");
      return;
      }
    var url="Task123.jsp";
    url=url+"?q="+str;
    url=url+"&sid="+Math.random();
    xmlhttp.onreadystatechange=stateChanged;
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
    }
    
    function stateChanged()
    {
    if (xmlhttp.readyState==4)
      {
      document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
      }
    }
    
    function GetXmlHttpObject()
    {
    if (window.XMLHttpRequest)
      {
      // code for IE7+, Firefox, Chrome, Opera, Safari
      return new XMLHttpRequest();
      }
    if (window.ActiveXObject)
      {
      // code for IE6, IE5
      return new ActiveXObject("Microsoft.XMLHTTP");
      }
    return null;
    }

    Third page : Task123.jsp

    Code:
    <%@ page import="java.sql.*" %>
    <%
    String connectionURL = "jdbc:mysql://localhost/new";
    Connection connection = null;
    Statement statement;
    ResultSet rs;
    %>
    
    <html>
    <head>
    <script type="text/javascript" src="Task113.js">
    </script>
    </head>
    <body>
    
    <%
    String s=request.getParameter("q");
    Class.forName("com.mysql.jdbc.Driver");
    connection = DriverManager.getConnection(connectionURL, "root", "cng");
    statement = connection.createStatement();
    rs = statement.executeQuery("SELECT prid FROM project where pid='"+s+"'");
    String targetId=request.getParameter("q");
    %>
    
    <form name="myform1" method="post" action="Task113.jsp">
    Projects for Program are :&nbsp;&nbsp;
    <select name="project" id="project" style="width: 100px" [B]onChange="showCustomer6(document.myform1.project.value)[/B]">
    
    <%
    while(rs.next())
    {
    String result=rs.getString(1);
    String home="unbounded";
    %>
    	<%
    	out.println(s+" consist of :<br>");
    	if(targetId.equalsIgnoreCase("unbounded"))
    	{%>
    	<option value="<%= home %>">
    	<% out.println(home); %>
    	</option>
    	<%}
    	else if(targetId.equalsIgnoreCase("Program 1"))
    	{%>
    	<option value="<%= result %>">
    	<%  out.println(result); %>
    	</option>
    	<%}
    	else
    	{%>
    	<option value="<%= result %>">
    	<%	out.println(result); %>
    	</option>
    	<%}
    }
    	%>
    </select>
    
    </form>
    </body></html>
    Forth page : Task113.js

    Code:
    var xmlhttp
    
    function showCustomer6(str)
    {
    	alert("1");
    xmlhttp=GetXmlHttpObject();
    if (xmlhttp==null)
      {
      alert ("Your browser does not support AJAX!");
      return;
      }
    var url="dummy.jsp";
    url=url+"?q1="+str;
    url=url+"&sid="+Math.random();
    xmlhttp.onreadystatechange=stateChanged;
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
    }
    
    function stateChanged()
    {
    if (xmlhttp.readyState==4)
      {
      document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
      }
    }
    
    function GetXmlHttpObject()
    {
    if (window.XMLHttpRequest)
      {
      // code for IE7+, Firefox, Chrome, Opera, Safari
      return new XMLHttpRequest();
      }
    if (window.ActiveXObject)
      {
      // code for IE6, IE5
      return new ActiveXObject("Microsoft.XMLHTTP");
      }
    return null;
    }
    Fifth and the Final page : dummy.jsp

    Code:
    <%@ page import="java.sql.*" %>
    <%
    String connectionURL = "jdbc:mysql://localhost/new";
    Connection connection = null;
    Statement statement1 = null;
    ResultSet rs1 = null;
    %>
    
    <html><body>
    
    <%
    	String s1=request.getParameter("q1");
    	out.println("String is "+s1);
    Class.forName("com.mysql.jdbc.Driver");
    connection = DriverManager.getConnection(connectionURL, "root", "cng");
    statement1 = connection.createStatement();
    rs1 = statement.executeQuery("SELECT * FROM task1 where prid='"+s1+"'");
    %>
    <table border=1>
    <%while(rs1.next()) {%>
    <tr><td>
    <%
    	out.println(rs1.getString("tid")+"<br>");
    	%>
    </td>
    </tr><tr><td>
    <%
    	out.println(rs1.getString("tname")+"<br>");
    	%>
    </td>
    </tr><tr><td>
    <%
    	out.println(rs1.getString("prid")+"<br>");
    	%>
    </td>
    </tr><%
    }
    %></table><%
    
    rs1.close();
    
    %>
    
    </body></html>.
    eagerly waiting for ur quick reply.thanks in advance.
    Last edited by gits; Feb 26 '10, 02:55 PM. Reason: added code tags
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    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

    Comment

    • ganihskmca
      New Member
      • Feb 2010
      • 5

      #3
      sorry i caunt get you..........pl s be elaborate...... ......

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        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 ...

        Comment

        Working...