I want to display the list of words when I type the first letter of the word in a text box. I am using jsp to design the form. I want ajax sample to achieve this feature. Its like google search. Please help. Thanks in advance.
JSP Ajax Autocomplete box
1)autocomplete.jsp:
<%@page import="java.sql.*"%> <html> <head> <script type="text/javascript"> function showData(value){ xmlHttp=GetXmlHttpObject() var url="auto.jsp"; url=url+"?name="+value; xmlHttp.onreadystatechange=stateChanged xmlHttp.open("GET",url,true) xmlHttp.send(null) } function stateChanged() { if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ var showdata = xmlHttp.responseText; document.getElementById("mydiv").innerHTML= showdata; } } 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> <form name="employee"> <input type="text" name="name" id="name" onkeyup="showData(this.value);"><br> <div id="mydiv"></div> </table> </body> </html>
2)auto.jsp:
<%@ page import="java.sql.*" %> <% String name = request.getParameter("name").toString(); String buffer="<div>"; try{ Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root"); Statement st=con.createStatement(); ResultSet rs=st.executeQuery("select * from data where name like '"+name+"%'"); while(rs.next()) { buffer=buffer+rs.getString("name")+"<br>"; } buffer=buffer+"</div>"; response.getWriter().println(buffer); } catch (Exception e) { System.out.println(e); } %>
hi,
i tried the same code above.
it is displaying the data.
but how can i click and select a specific data from the list? somebody pls help me..
thats rite the objective is still not completed , i also want to select the data in textbox when i click on the items coming after search.........plz help thanks
Did you guys found any solution for the above.....Even I am looking for the same , please help....
Ads