Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

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/ajax-drop-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:

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.


<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

<%@ 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 ..
[4176 byte] By [vitalstrike82] at [2007-11-11 9:36:01]
# 1 Re: Ajax - Cant pass value from jsp page to js
Im guessing your problem lies in trying to access the innerHTML property for a text field or textarea: it doesnt exist. Use document.getElementById("txtProject").value=xmlHttp.responseText instead.
Alantin at 2007-11-11 23:42:31 >