
function getentityshortname(val) {
alert("entered");
var centrecode=val; var unitcode=val;
var postString = "¢recode="+centrecode; var postString = "&unitcode="+unitcode;
// alert(postString);
var url='getentitydata.jsp';
//window.open(url);
//alert(url);
if (window.XMLHttpRequest)
{
req = new XMLHttpRequest();
}
if (window.ActiveXObject)
{
req = new ActiveXObject("Microsoft.XMLHTTP");
}
req.onreadystatechange = entityshortnamedetails; //handling response
req.open("post", url, true);
req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
req.setRequestHeader('Content-length', 'parameters.length');
req.send(postString);
//document.forms[0].action=url;
//document.forms[0].submit();
} /******************************/ function entityshortnamedetails() { if (req.readyState == 4) { if (req.status == 200) { var rset=new Array(); rset=req.responseText; rset=rset.replace(/^\s+|\s+$/g,""); var list2=new Array(); list2=rset.split('~'); var spacetrim = /,\s+/;
if(document.employee.cmbentity.length > 0) //loop for clearing already existed values in a combo
{
document.employee.cmbentity.length= 0;
}
/*values assignment */
document.employee.cmbentity.options[0] = new Option();
document.employee.cmbentity.options[0].text="--select--";
for(var i=0,j=1; i < list2.length-1; i++,j++)
{
document.employee.cmbentity.options[j] = new Option();
document.employee.cmbentity.options[j].text=list2[i].split(spacetrim);
}
/*values assignment */
}
else
{
alert ("Unable to get Data" +req.responseText);
}
}
} /******************************/ can we use val twice in the above

Here is an example of ajax that will display the database values in the textboxes by getting the id through another textbox.
1)ajax.jsp:
<%@page import="java.sql.*"%>
<html>
<head>
<script type="text/javascript">
function showData(){
xmlHttp=GetXmlHttpObject()
var id=document.getElementById("id").value;
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(":");
if(strar.length>1)
{
var strname = strar[1];
document.getElementById("name").value= strar[1];
document.getElementById("address").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>
<br><br>
<table >
<tr><td>Client ID:</td><td><input type="text" id="id" name="id" onkeyup="showData();"></td></tr>
<tr><td>Name:</td><td><input type="text" id="name" name="name"></td></tr>
<tr><td>Address:</td><td><input type="text" id="address" name="address"></td></tr>
</table>
</body>
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 stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("Select * from addclients where clientid ="+id+"");
while(rs.next())
{
data = ":" + rs.getString("name") + ": " + rs.getString("address");
}
out.println(data);
}
catch(Exception e){
System.out.println(e);
}
%>
</html>
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.