
i have a select list containing elements, say EmpCode. there are 2 fields below it EmpName and DeptName.
As i select EmpCode from the select list, the corresponding EmpName and DeptName should be displayed automatically in empty text fields.
I am using struts 1.2 framework for the application.
Kindly reply asap.
Thanks & regards.

1)ajax.jsp:
<%@page import="java.sql.*"%>
<html>
<head>
<script type="text/javascript">
function showData(id){
xmlHttp=GetXmlHttpObject()
var url="getdata.jsp";
url=url+"?id="+id;
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}
function stateChanged(){
if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
var showdata = xmlHttp.responseText;
var strar = showdata.split(":");
document.getElementById("name").value= strar[1];
document.getElementById("dept").value= strar[2];
}
}
function GetXmlHttpObject(){
var xmlHttp=null;
try {
xmlHttp=new XMLHttpRequest();
}
catch (e) {
try {
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
</script>
</head>
<body>
<form name="employee">
<br><br>
<table >
<div id="mydiv"></div>
<tr><td><b>Select:</b></td><td><select onchange="showData(this.value);">
<%
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from emp ");
while(rs.next()){
%>
<option value="<%=rs.getInt("EMP_NO")%>"><%=rs.getInt("EMP_NO")%></option>
<%
}
%>
</select></td></tr>
<tr><td><b>EMP Name:</b></td><td><input type="text" id="name" ></td></tr>
<tr><td ><b>DEPT Name:</b></td><td><input type="text" id="dept" ></td></tr>
</table>
</form>
</body>
</html>
2)getdata.jsp:
<%@ page import="java.sql.*" %>
<%
int id = Integer.parseInt(request.getParameter("id"));
String data ="";
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from emp where EMP_NO="+id+"");
while(rs.next())
{
data =":"+rs.getString("EMP_NAME")+":"+rs.getString("DEPT_NAME");
}
out.println(data);
System.out.println(data);
}
catch (Exception e) {
System.out.println(e);
}
%>
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.