i want 2 dropdown list 1- CLASS 2-SECTION both are should come from database. and if i select a class suppose class a the corresponding section dropdown list should retrieve the section respective to that class from database. please help by providing the code in jsp as soon as possible.its urgetnt.
thank you sir, but its nt working in my pc and that to i am nt aware of the ajax so please send me the same project in jsp
1)country.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="state.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("state").innerHTML=xmlHttp.responseText } } </script> </head> <body> <select name='country' onchange="showState(this.value)"> <option value="none">Select</option> <% Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root"); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("Select * from country"); while(rs.next()){ %> <option value="<%=rs.getString(1)%>"><%=rs.getString(2)%></option> <% } %> </select> <br> <div id='state'> <select name='state' > <option value='-1'></option> </select> </div> </body> </html>
2)state.jsp:
<%@page import="java.sql.*"%> <% String country=request.getParameter("count"); String buffer="<select name='state' ><option value='-1'>Select</option>"; try{ Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root"); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("Select * from state where countryid='"+country+"' "); while(rs.next()){ buffer=buffer+"<option value='"+rs.getString(1)+"'>"+rs.getString(3)+"</option>"; } buffer=buffer+"</select>"; response.getWriter().println(buffer); } catch(Exception e){ System.out.println(e); } %>
For the above code, we have created two database tables:
CREATE TABLE `country` ( `countryid` bigint(255) NOT NULL auto_increment, `countryname` varchar(255) default NULL, PRIMARY KEY (`countryid`)); CREATE TABLE `state` ( `stateid` bigint(255) NOT NULL auto_increment, `countryid` int(255) default NULL, `state` varchar(255) default NULL, PRIMARY KEY (`stateid`));
Hai friends I have a doubt in above code that How can we get those selected values into our SERVLET.
I mean i could display the values dynamaically but while submitting the form,the selected values getting NULL.
I replaced the code in above
";
as
";
still i could not...please provide solution
<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <%@ page language="java" %> <%@ page import="java.sql.*" %> <html> <script> function change(){ var cid=document.getElementById("books").selectedIndex; var val = document.getElementById("books").options[cid].text; window.location.replace("http://localhost:8080/faculty/book1.jsp?id="+cid+"&&value="+val); } function extract(){ var ide=document.getElementById("info").selectedIndex; var bookname = document.getElementById("info").options[ide].text; window.location.replace("http://localhost:8080/faculty/book1.jsp?book="+bookname); } function extractsub(){ var sde=document.getElementById("subject").selectedIndex; var subject = document.getElementById("subject").options[sde].text; window.location.replace("http://localhost:8080/faculty/book1.jsp?subject="+subject); } function enableDisable() { if(document.book1.holiday.checked){ document.book1.txt.disabled = false; } else { document.book1.txt.disabled = true; } } </script> <input type="text" name="txt"> <form name="book1" action="try.jsp" method="post" > <%! Connection conn = null; ResultSet rs =null; Statement st=null; String query=""; %> <% String value=request.getParameter("value"); Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/newdatabase","root","root"); st = con.createStatement(); rs = st.executeQuery("select * from book"); %> <select id="books" name="books" onchange="change();"> <option value="0">--Please Select--</option> <% while(rs.next()) { %> <option value="<%=rs.getString("books")%>"><%=rs.getString("books")%></option> <% if(rs.getString("books").equals(value)) { %> <option value="<%=value%>" selected disabled><%=value%></option> <% } } %> </select> <select id="info" name ="info"> <option value="0">--Please Select--</option> <% String id=request.getParameter("id"); rs=st.executeQuery("select * from bookinformation where bookid='"+id+"'"); while(rs.next()) { %> <option value="<%=rs.getString("id")%>" ><%=rs.getString("booknames")%></option> <% } %> </select> HOLIDAY<input type ="checkbox" name="holiday" value="HOLIDAY" onclick="radio.disable=!this.checked"> <br><h3>SUBJECTS</h3> <table> <% String sid=request.getParameter("id"); rs=st.executeQuery("select * from subject where id='"+sid+"'"); while(rs.next()) { %> <tr><td><%=rs.getString("subject")%></td><td><input type="radio" value="<%=rs.getString("subject")%>" name="subject" onchange="extractsub();"></td> <% } %> </tr> </table> <select id="faculty_name" name="faculty_name"> <option value="0">--Please Select--</option> <% String faculty_name=request.getParameter("faculty_name"); rs=st.executeQuery("select * from faculty_name "); while(rs.next()) { %> <option value="<%=rs.getString("faculty_name")%>"><%=rs.getString("faculty_name")%></option> <% } %> </select> <input type="submit" value="SUBMIT"> </form> </body> </html>
i have included this coding my project. this coding will create 2 dropdown and dependent on each other. and u'll get a list of subjects in radio button according to the selection made in first drop down list.
Ads