Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
    Bookmark Topic Watch Topic
  • New Topic

Ajax in JSp

 
arjun kt
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Report post to moderator
hii friendz ma problem is am using ajax for populating user id and name from data base my sql..... bt d datas are not coming please help mi... ma codes are below
 
arjun kt
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Report post to moderator
<%@ page import="java.sql.*" %>
<html>
<head>
<script type="text/javascript">
function showUser(user_value)
{
if(document.getElementById("user_id").value!="-1")
{
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return
}
var url="getuser.jsp"
url=url+"?user_id="+user_value

xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)

}
else
{
alert("Please Select User Id");
}
}

function stateChanged()
{
document.getElementById("username").value ="";
document.getElementById("user_id").value ="";
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{

var showdata = xmlHttp.responseText;
var strar = showdata.split(":");

if(strar.length==1)
{
document.getElementById("user_id").focus();
alert("Please Select User Id");
document.getElementById("username").value =" ";
document.getElementById("user_id").value =" ";

}
else if(strar.length>1)
{
var strname = strar[1];
document.getElementById("User_id").value= strar[2];
document.getElementById("username").value= strar[1];
}

}
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
//Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
</script>

</head>
<body>
<form name="user">
<br><br>
<table border="0" width="400px" align="center" bgcolor="#CDFFFF">
<div id="mydiv"></div>
<tr><td><b>Select User Id</b></td><td>
<select name="suser_id" onchange="showUser(this.value);">
<option value="-1">Select</option>
<%

Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "user_register";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "root";

int sumcount=0;
Statement st =null;
try {
Class.forName(driver).newInstance();

conn = DriverManager.getConnection(url+dbName,userName,password);
String Query = "select * from knp_usermaster";

st = conn.createStatement();
ResultSet rs = st.executeQuery(Query);
%>

<%
while(rs.next())
{
%>

<option value="<%=rs.getString(1)%>"><%=rs.getString(1)%></option>

<%
}
%>

<%

}
catch (Exception e) {
e.printStackTrace();
}

%>

</select>
</td></tr>


<tr><td><b>User Id:</b></td><td><input type="text" name="user_id" id="user_id" value=""></td></tr>
<tr><td><b>UserName:</b></td><td><input type="text" name="user_name" id="user_name" value=""></td></tr>

</table>
</form>

<table border="0" width="100%" align="center">


<br>
<br>


</table>
</body>
</html>
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Report post to moderator
Arjun Kt wrote: hii friendz ma ...... bt d datas ..... mi... ma codes are below

Please UseRealWords

You have to tell the details, Where are you facing problem, Have you tried to use multiple "alert()' statement to debug, where its going wrong ?

One suggestion, Use it readyState once, like



And please use CodeTags
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64824
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Report post to moderator
Please do not cross-post the same question in multiple forums. It wastes people's time when multiple redundant conversations take place. Please read this for more information.
 
    Bookmark Topic Watch Topic
  • New Topic