2
Contributors
14
Replies
16
Views
6 Years
Discussion Span
Last Post by pritaeas
Featured Replies
0

TO : pritaeas

Thanks for replying...but actually i want only by using Ajax,JSp,MySql,Html........

0

city.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="div_city.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("div_city").innerHTML=xmlHttp.responseText
      }
      }


      </script>
 </head>
 <body>
 
City<input type="text" name="city" id="city" onkeyup="showState(this.value)"></td>
 <br>
 <div id='div_city'>
 </div>
   </body></center>
 </html>

div_city.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<%@page import="java.sql.*"%>
 <%
 String name=request.getParameter("count");
 String buffer="<div>";
 Class.forName("com.mysql.jdbc.Driver").newInstance();
 Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Prashanth","root","");
 Statement stmt = con.createStatement();
 ResultSet rs = stmt.executeQuery("Select cityname from city where cityname LIKE '%"+name+"%'");
   while(rs.next()){
   buffer=buffer+"<input type='text' name='city' value="+rs.getString("cityname")+">"+"<br>";
   }
 buffer=buffer+"</div>";
 response.getWriter().println(buffer);
 %>

This is my actual code but its not working.......

Edited by ypks_destiny: n/a

0

It doesnt give errors but live search is not happening...for example if i type "a" letter in text field it searches the database and display 5 different text fields and i am onable to select in my city field....

0

I think the input's are a weird choice for showing your possibilities. I think a normal div containing links would be a more sensible choice. You'll need to add some javascript to each link, so when it is clicked, it will be copied to main editbox. Have a look at this demo.

0

Re: ajax live search
I think the input's are a weird choice for showing your possibilities. I think a normal div containing links would be a more sensible choice. You'll need to add some javascript to each link, so when it is clicked, it will be copied to main editbox. Have a look at this demo.

that demo you gave was a good one....but it is not taking values from database .....I want the values to be taken from database.....

Edited by ypks_destiny: n/a

0

There are a lot of examples in this forum on how to do that. Just search for XMLHttpRequest.

i tried each and every one but i couldn't find it...all of them are in dropdown list only i want it in text field search

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.