Tom
Tom

Reputation: 771

auto complete text boxes in jsp

Ok i have found a solution regarding my problem. I have three text boxes in my jsp page, when i am entering any data on first text box then control is going to get.jsp to retrieve data from database and is filling 2nd text box. But I want that data will go from first and second text boxes at a time to get.jsp to auto fill 3rd text box , but i was trying but data was going individually from either 1st or 2nd text boxes.

----auto.jsp----------

<%@page import="java.sql.*"%>
<html>
<head>  
<script language="javascript" type="text/javascript">  
var xmlHttp  
var xmlHttp
function showState(str){ 
if (typeof XMLHttpRequest != "undefined"){
xmlHttp= new XMLHttpRequest();
   }
   else if (window.ActiveXObject){
xmlHttp= new ActiveXObject("Microsoft.XMLHTTP");
    }
if (xmlHttp==null){
alert ("Browser does not support XMLHTTP Request")
return
} 
var url="get.jsp";//goes to get.jsp
url += "?count=" +str;
xmlHttp.onreadystatechange = stateChange;
xmlHttp.open("GET", url, true);
xmlHttp.send(null);
}
function stateChange(){   
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){   
document.getElementById("country").innerHTML=xmlHttp.responseText;  
}   
}   
</script>  
</head>  
 <body>  
 <input id="name" type="text" name="name" onkeyup="showState(this.value)">
<br>  
<div id='country'>  
</div>  
</body> 
</html>

--------get.jsp-------------

<%@page language="java" import ="java.sql.*" %>  
 <%  
 String name=request.getParameter("count");  
 String buffer="<div>";  
 //Got value from database 


while(rs.next()){
buffer=buffer+rs.getString(2)+"<br>";  
}  
buffer=buffer+"</div>";  
response.getWriter().println(buffer);  
%>

Here by request.getParameter("count"); I am getting only one text box value, but how can i get multiple value from auto.jsp by entering data in first and second text boxes.

Upvotes: 0

Views: 3416

Answers (1)

prajeesh kumar
prajeesh kumar

Reputation: 1966

Just call the showState() function on your onkeyup and in the javascript function append the textbox's value.

var url="get.jsp";//goes to get.jsp
url += "?count=" +document.getElementById(<first textbox's id>).value;
url += "&secondVal="+document.getElementById(<second textbox's id>).value;

and you can get the value like you did

String secondVal=request.getParameter("secondVal"); 

Upvotes: 1

Related Questions