Hi, In my project we have to enter a productid which is first need to be searched into database. if it already exists then it will autopopulate its corresponding producttype,productprice into textboxes when we press tab or click on the producttype(which is first textbox related to product_id).This is need to be done before actual submission of form so kindly provide me related code.
1)checkid.jsp:
<%@page import="java.sql.*"%> <html> <head> <script type="text/javascript"> function checkID(){ var value=document.getElementById("id").value; xmlHttp=GetXmlHttpObject(); var url="id.jsp"; url=url+"?id="+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; alert(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; } function showData(){ xmlHttp=GetXmlHttpObject() var id=document.getElementById("id").value; var url="data.jsp"; url=url+"?id="+id; xmlHttp.onreadystatechange=stateChanged1 xmlHttp.open("GET",url,true) xmlHttp.send(null); } function stateChanged1(){ if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ var showdata = xmlHttp.responseText; var strar = showdata.split(":"); if(strar.length>1){ var strname = strar[1]; document.getElementById("type").value= strar[1]; document.getElementById("price").value= strar[2]; } } } </script> </head> <body> <br><br> <table > <tr><td>ID:</td><td><input type="text" id="id" name="id" onkeyup="checkID();"></td></tr> <tr><td>Product Type:</td><td><input type="text" id="type" name="name" onclick="showData();"></td></tr> <tr><td>Price:</td><td><input type="text" id="price" name="address"></td></tr> </table> </body> </html>
2)id.jsp:
<%@ page import="java.sql.*" %> <% String id = request.getParameter("id").toString(); System.out.println(id); int count=0; String data =""; 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 item where itemid='"+id+"'"); while(rs.next()) { count++; } if(count>0){ data="It is a valid id"; } else{ data="It is not a valid id"; } out.println(data); System.out.println(data); } catch(Exception e) { System.out.println(e); } %>
3)data.jsp:
<%@ page import="java.sql.*" %> <% String id = request.getParameter("id").toString(); System.out.println(id); String data =""; 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 item where itemid='"+id+"'"); while(rs.next()) { data = ":" + rs.getString("item") + ": " + rs.getString("price"); } out.println(data); System.out.println(data); } catch(Exception e) { System.out.println(e); } %>