Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners jsp- database dependent dropdown list
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


aayushi saxena
jsp- database dependent dropdown list
4 Answer(s)      3 years and 2 months ago
Posted in : Java Beginners


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.


Advertisement
View Answers

May 31, 2013 at 4:49 PM


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



October 27, 2011 at 11:28 AM


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`));


November 1, 2011 at 11:45 AM


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



November 1, 2011 at 11:54 AM


<%@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.



Related Tutorials/Questions & Answers:
jsp- database dependent dropdown list
jsp- database dependent dropdown list   i want 2 dropdown list 1... 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
3 dropdown list from the database using JSP
3 dropdown list from the database using JSP  Hi, I'm new to JSP I want to create 3 dropdown list each depend on the other and get the options from the database using JSP
Advertisements
getting and storing dropdown list in database in jsp
getting and storing dropdown list in database in jsp  i have a drop down list to select book from database. i'm able to retrieve dropdown list from database. but unable to store the selected value in database table. please help
dropdown list in jsp
dropdown list in jsp  hai, i have static dropdown list.. i want to get the selected value in string variable without jsp regards asha
how to add data dynamically from database into a dropdown list in a jsp
how to add data dynamically from database into a dropdown list in a jsp  hi friends i am doing a project in jsp and oracle as my database.In my... in storing them into the database,this registration form contains name
JSP Get Data Into Dropdown list From Database
JSP Get Data Into Dropdown list From Database In this section we will discuss about how to fetch data dynamically into the dropdown list in JSP... data from the database and set it into the dropdown list in JSP using MySQL
Hi, I'm new to JSP I want to create 3 dropdown list each depend on the other and get the options from the database using JSP
Hi, I'm new to JSP I want to create 3 dropdown list each depend on the other... to create 3 drop dropdown list each depend on the other and get the options from the database using JSP. like country,state,city..please guide me
Dependent drop down list
Dependent drop down list  hi,i am trying to design a form on which 2 dependent drop down list is used my code is successful but when i select class... is possible to manage four dropdown dependnt list? Thanks a lot. regards Nico
DropDown list
DropDown list  how to get mysql database values into dropdown usign java servlet and ajax?   Here is a jsp code that displays the database values into dropdown list. 1)country.jsp: <%@page import="java.sql.*"%>
Want code for 5 dependent dropdown in php
Want code for 5 dependent dropdown in php  I have 5 dds, if i select value from first one then related values are displayed in 2 dd and so on .plz help   Please go through the following link: Visit Here The above link
jsp -sevlet connecting to database using dropdown
jsp -sevlet connecting to database using dropdown  How can I get my dropdown list from oracle database and then submit it to another table in JSP. I... to the database and fetches an array of strings from a database table and then sends
how to use dropdown list in JSP and display value in same page
how to use dropdown list in JSP and display value in same page  I have a Dropdown list with some values say "A", "B" and "C" When the user selects... in the same page Value selected = C How can we do this in a JSP page
struts dropdown list
struts dropdown list   In strtus how to set the dropdown list values from database ? I have a ArrayList object and set this object in dropdown jsp page using struts ? please send me jsp code... sample code: **Action
Inserting values into a database table of selected DropDown in jsp.
Inserting values into a database table of selected DropDown in jsp.  http://www.roseindia.net/answers/viewqa/Ajax/15250-DropDown-in-ajax+jsp.html Thank you for this program, it helped me alot to learn. I have done it. now i want
dropdown list in jsf
dropdown list in jsf  I want to add a list box to display the country... then in the second list box it will display the states related to India only and the flow... with and without using database
Populate dropdown menu from database using jsp and servlet
Populate dropdown menu from database using jsp and servlet  please i need code to populate dropdown menu from mysql database using jsp and servlet. thanks
How to access Contacts from gmail using LDAP in JSP dropdown list
How to access Contacts from gmail using LDAP in JSP dropdown list  HI, I am creating a JSP page in which i have to make a dropdown box, and access gmail contacts in that drop downlist using LDAP. Can any one plz help me out
JSP Dependent Drop Down Menu
JSP Dependent Drop Down Menu  Hey Guy/Gals! I need someone help...; The given code will help you to create a dependent dropdown box. 1... menu and it's selection. So in my database I have a category table that has a ID
creating dropdown lists in jsp
creating dropdown lists in jsp  i want to create two dropdown list which are dependent that is the first box choice have to evaluate the second boxs options
Retrieval of Dropdown list
Retrieval of Dropdown list  Hi frnds... Am having problem with the below code... I have retrieved data from the mysql database into dropdownlist ... for eg, let A1,A2,A3,A4 be the values in dropdown.. if i click A1
how can i send a mail to a particular user from a many user dropdown list in jsp
how can i send a mail to a particular user from a many user dropdown list in jsp  how can i a sent a user question to a particular person from a drop down list in jsp
dropdown list and text fields in php
dropdown list and text fields in php  How could I use php to populate text fields by selecting a name of a business from dropdown list? Those text... number, description, and opening times from a mysql database. If you can, post
jsp dropdown refresh - JSP-Servlet
jsp dropdown refresh  Sir Thank u for your jsp code for dependent dropdown list, i used it in different table for data extraction but the probleam during data extraction when i select first drop down its refresh then 2nd
getting values from dropdown list
getting values from dropdown list  I am having a dropdown list which has hardcoded values ( we need it hardcoded only and will not be populated from the database) My question is when i select a particular value it should be pass
Dropdown Checkbox list
Dropdown Checkbox list This tutorial explains how to create jquery Dropdown Checkbox list in JSP.  This example is created in eclipse IDE and run... jquery Dropdown Checkbox list. The application directory structure should look like
inserting dropdown values into database table
inserting dropdown values into database table   hi i want to insert dropdown values into a database table by using jsp
Passing Multi select list box values using ajax to get values to dependent list box
Passing Multi select list box values using ajax to get values to dependent list box  Hi, I have a jsp page which has 3 dependent list boxes... the dependent values to the immediate next list box. Please help me
how to set value of dropdown list
how to set value of dropdown list  Hello Sir, I'd solved the earlier problems somehow but now this time another problem arised when I want to pass the value from a link to a dropdown list. I want to set the dropdown list selected
how to set value of dropdown list
how to set value of dropdown list  Hello Sir, I'd solved the earlier problems somehow but now this time another problem arised when I want to pass the value from a link to a dropdown list. I want to set the dropdown list selected
retrive the data from access database to drop down list box in jsp
retrive the data from access database to drop down list box in jsp  hai, im new to jsp now im using the jsp along with access database.in table i load all the data's i need to retrive the data from database to dropdown list box
store dropdown box values in database server
store dropdown box values in database server  how to store dropdown box values in database server in jsp
 

 

 

DMCA.com