dcsimg


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Ajax - Cant pass value from jsp page to js

  1. #1
    Join Date
    Jul 2006
    Posts
    3

    Ajax - Cant pass value from jsp page to js

    Hi, i have actually use an example from the tutorial to implement Ajax into my jsp project. The example i get from http://www.commoncoder.com/tutorials...p-down-box.asp.

    I want the program to works in this way where user selects a data in the list box and the final results will be display in the textbox.

    The example from the tutorial is almost the same from my project except that the values retrieve from the jsp page will be display in the textbox instead of the div.

    This are the codes:
    In the javascript:


    Code:
    var xmlHttp 
    function showResults(str) 
    
    { 
    
    xmlHttp=GetXmlHttpObject() 
    alert("read this first" +xmlHttp); 
    if (xmlHttp==null) 
    { 
    alert ("Your browser does not support the XMLHttpRequest object.") 
    return 
    } 
    
    var url="getProject.jsp" 
    url=url+"?q="+str 
    url=url+"&sid="+Math.random() 
    alert("URL = " + url); 
    xmlHttp.onreadystatechange=stateChanged 
    xmlHttp.open("GET",url,true) 
    xmlHttp.send(null) 
    alert("read this" +xmlHttp); 
    } 
    
    function stateChanged() 
    { 
    alert("read this2"); 
       if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") 
      { 
      document.getElementById("txtProject").innerHTML=xmlHttp.responseText 
      } 
    
    } 
    
    function GetXmlHttpObject() 
    { 
    alert("read this3"); 
     var objXMLHttp=null 
      
      try { 
        objXMLHttp = new ActiveXObject("Msxml2.XMLHTTP"); //later IE 
      } catch (e) { 
      try { 
        objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP"); //earlier IE 
      } catch (e) { 
      objXMLHttp = null; 
      }
    In the client page, where the select list box named selSite and textbox name txtProject. This is the client page where user selects an item in the listbox and display the result in the textbox. Please do ignore the vector and if statement as i'm retrieveing the data from the database to be display in the list box.


    Code:
    <html> 
    <body> 
    <input type="text" name="txtProject" id="txtProject"  size="20">    </td> 
    <% 
       Vector storageSite = new Vector(); 
       newSiteObj siteObj = new newSiteObj(); 
       storageSite = siteObj.retrieveSite(); 
       System.out.println("In here"); 
    %> 
    <select name="selSite" WIDTH="230" STYLE="width: 230px" id="selSite" onChange="return showResults(this.value)"> 
    <% 
    if(storageSite.size()>0) 
    { 
    for(int i=0; i<storageSite.size(); i++) 
    { 
    newSiteBean siteBean = (newSiteBean) storageSite.elementAt(i); 
    out.println("<option name = idSite,  value='"+ siteBean.getsite()+ "'>" +siteBean.getsite()+ "</option>"); 
    } 
    } 
    %> 
        </select>

    Lastly this is the jsp page where the retrieving of data is done. I have check the running of the code and the retrieving of data is successfully done and i store the data in the variable str_project. Now i wan to return the value of this variable back so it can be display on the client page but not successfully. Can any one help me with this. Thanks


    Code:
    <%@ page language = "java"  import="java.lang.*, 
    java.util.*, 
    java.util.Date, 
    java.text.DateFormat, 
    java.text.SimpleDateFormat, 
    com.IMS.IMSObj.invDetailsObj, 
    com.IMS.IMSObj.newSupplierObj, 
    com.IMS.bean.newSupplierBean, 
    com.IMS.IMSObj.newSiteObj, 
    com.IMS.bean.newSiteBean, 
    com.IMS.bean.InvDetailsBean" %> 
    
    <% 
    //System.out.println('In jsp Page"); 
       String str_project; 
        
       String search = request.getParameter("q"); 
       System.out.println("Search = " +search); 
        
       newSiteObj siteObj = new newSiteObj(); 
       siteObj.retrieveSiteForAjax(search); 
       System.out.println("In here"); 
        
       str_project = siteObj.getproject(); 
       System.out.println("STRProject " +str_project); 
       out.println(str_project); 
    %> 
    <html> 
    <body> 
    out.println(str_project); 
    </body> 
    </html>
    Thanks ..

  2. #2
    Join Date
    Dec 2004
    Posts
    32
    I´m guessing your problem lies in trying to access the innerHTML property for a text field or textarea: it doesn´t exist. Use document.getElementById("txtProject").value=xmlHttp.responseText instead.

Similar Threads

  1. Replies: 2
    Last Post: 09-07-2006, 06:10 AM
  2. Replies: 0
    Last Post: 07-01-2006, 02:45 AM
  3. Replies: 0
    Last Post: 12-20-2005, 06:29 AM
  4. Replies: 0
    Last Post: 12-02-2005, 12:54 PM
  5. Must I compile a bean for a JSP page?
    By Alison in forum Java
    Replies: 1
    Last Post: 07-26-2000, 02:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center