aspose file tools*
The moose likes JSP and the fly likes Ajax in JSp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Reply locked New topic
Author

Ajax in JSp

arjun kt
Greenhorn

Joined: Feb 14, 2009
Posts: 5
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

Joined: Feb 14, 2009
Posts: 5
<%@ 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

Joined: Feb 19, 2008
Posts: 2902
    
    1

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

Joined: Jan 10, 2002
Posts: 61420
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Ajax in JSp