heyy can ny 1 help me.....m wrking on jsp using ajax......i built a small application...as m new to ajax.....i built 2 field....FNAME & Lname and when my application used to do is..when you enter ur first name.....n press the tab button...the last name shld come from data base......but its nt wrking.....can ny 1 help me !!
file name-"Myform.html" -
<html>

<head>
<title></title>
</head>

<body bgcolor="#008000">
<centre>
                                       
ENTER THE WORLD
</centre>

<script type="text/javascript">
function ajaxFunction1()
{
alert("sss");

var xmlhttp;
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else
{
alert("Your browser does not support XMLHTTP!");
}

var url="Lname.jsp?fname=shankey";

alert(url);

xmlhttp.onreadystatechange=XLNAME()"{
xmlhttp.open("GET",url,true);
xmlhttp.send(null);

}

function XLNAME(){
alert("hello");
if(xmlhttp.readyState==4)
{
document.myform.lname.value=xmlhttp.responseText;
}
}

}
</script>

<form name="myform">

F.Name:<input type="text" name="fname" onBlur="ajaxFunction1();" >

L.Name:<input type="text" name="lname" >
</form>

</body>
</html>

and

file name-"Lname.jsp"

<%@ page language="java" import="java.sql., java.io." %>
<%@ page import="java.util.*" %>

<%
String sn="";
String str1=request.getParameter("fname");
//String str2=request.getParameter("lname");
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.101:1521:ORCL","SHANKEY","SHANKEY");
Statement pst=con.createStatement();
String query="select Surname from DATA1 where Name='"str1"'" ;
//out.print(query);
ResultSet rst=pst.executeQuery(query);
while(rst.next()){
sn=rst.getString("Surname");
}

//else{
//response.sendRedirect("Myform.html");

out.println(sn);

//}

}catch(Exception e)
{
out.println("Exception:"+e);
}

%>

....................................can ny 1 help me out

Your code has some bugs. Have a look,
1. Variable var xmlhttp; must be placed outside the function.
2. Callback method,
xmlhttp.onreadystatechange=XLNAME;

<html>
<head>
     <title></title>
</head>
  <body bgcolor="#008000">
     <centre>
      ENTER THE WORLD
      </centre>

<script type="text/javascript">
  var xmlhttp;
 function ajaxFunction1(){
  
  if (window.XMLHttpRequest){
    // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
  } 
  else 
  if (window.ActiveXObject){
     // code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
   }
  else{
    alert("Your browser does not support XMLHTTP!");
  }

   var url="Lname.jsp?fname=" + document.getElementById("fname").value;
 
   xmlhttp.onreadystatechange=XLNAME;
   xmlhttp.open("GET",url,true);
   xmlhttp.send();
  }

  function XLNAME(){
    if(xmlhttp.readyState==4){
        alert(xmlhttp.responseText);
       document.myform.lname.value=xmlhttp.responseText;
    }
    
}
</script>

<form name="myform">
  F.Name:<input type="text" name="fname" onBlur="ajaxFunction1();" >
  L.Name:<input type="text" name="lname" >
</form>
</body>
</html>

Lname.jsp
1. Page directive is not form properly.
Use import="java.sql.*

<%@ page language="java" import="java.sql.*, java.io.*" %>

2. Select query string concatenation - .. Name='"str1"'"

String query="select Surname from DATA1 where Name='" + str1 + "'";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.