473,405 Members | 2,294 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

create a dropdown list based on selection from another dropdown list in jsp

I am doing a program in java .I have designed a java server page which has 3 drop down list's...country,state.I want to display only corresponding the values in drop down list state when a country is selected...(for example:-if India is selected,drop down list state must have only the name of states in India).I have created 2 tables country and state.Table country has country id and country name.Table state has state id,state name and a foreign key country id from table country.
Expand|Select|Wrap|Line Numbers
  1. String coun=request.getParameter("countryddl");ResultSet rs1=stmt.executeQuery("select * from country where countryname='"+coun+"'");
  2.                 while(rs1.next())
  3.                 {
  4. Integer counid=Integer.parseInt(rs1.getString("countryid"));
  5. ResultSet rs= stmt.executeQuery("select * from state where countryid="+counid);
  6.                %> <select name="stateddl">
  7.                      <option  value="0">Select</option>
  8.                <% while(rs.next())
  9.                 {
  10.                 %>
  11.                         <option><%= rs.getString("statename")%></option>
  12.  
  13.                     <% } %>
  14.                     </select>
  15. <% }
  16. }
  17.  
Full Code is attached with this message.

Please help me to complete this program...
Attached Files
File Type: docx CODE.docx (14.0 KB, 969 views)
May 23 '15 #1
6 9551
chaarmann
785 Expert 512MB
It is some years ago when I did exactly the same, so let me see what I remember.
First, do not fill the drop down boxes in JSP as what I see in your code above. Just create a two-dimensional JSON array in Javascript instead, like:
Expand|Select|Wrap|Line Numbers
  1. ["country1":{state1, state1}, "country2":{state3, state4, stae5}]
Then create the website with empty dop-down-lists for state and country and call a javascript method setCountry("country1") in the onLoad() that fills the drop-down-boxes with values from the JSON array. You should call the setCountry method also every time you select a new country from the drop-down box, so that the second box will be updated accordingly and only contain the states from the selected country.
May 28 '15 #2
Thank you for replying...But my problem is not solved.I want to get the drop down list populated from the tables country and state in data base.I don't know java script.How can i do this?Can you Please provide code in java script.
May 31 '15 #3
chaarmann
785 Expert 512MB
Maybe you should read about JSON and javascript. Then you will see that my email above exactly solves your problem. I mean, you don't read from database and produce HTML directly, but instead you read from database and produce JSON. Then you use Javascript to make HTML out of JSON.

But no need to reinvent the wheel, there is some javascript ready for you to plug it in.
Just use "jquery". See at http://www.sitepoint.com/16-jquery-s...-down-plugins/
Especially chapter "4. jQuery Chained Select".

Side remark:
You are lucky. In the past when I wrote my drop-down-box code for country and city, jquery did not even exist.
Jun 1 '15 #4
chapter "4. jQuery Chained Select" is giving an error page in the above link...can you please give an example of the code.
Jun 3 '15 #5
chaarmann
785 Expert 512MB
The link is down somehow. Just google for it ("jQuery Chained Select") and you will find a lot of links that are working with much more demos and code examples. For example http://www.appelsiini.net/projects/chained
Jun 8 '15 #6
In the example in this link, values are directly inserted to drop down list.I want to insert values into drop down list from a table.Does it make any difference?

Where should i place this code?in a java script??
$("#series").chained("#mark");
I have placed it in a java script.But have not got the output.
<script type="text/javascript">
$("#series").chained("#mark");
</script>
I don't know ajax and java script.Please provide code in detail...
Jun 12 '15 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Mark | last post by:
I have two dropdown lists. Both have autopostback set to true. In both dropdowns, when you select an item from the list, it redirects to the Value property of the dropdown. Nothing fancy. ...
11
by: abctech | last post by:
Hi, I am developing a webapplication using Javascript,JSP,Servlets. I have 2 dropdown lists in my form, the first one is autopopulated on form load , I have written this code in Jsp and its...
2
by: Johnny BeGood | last post by:
Hi All, I need to populate a list box and/or a dropdown list on a form. I have all the bits and pieces together, all bar the code which takes the result of a query and creates a list box. Any...
2
by: Shaileshsharma | last post by:
Hi, I am new in C# ,i have one problem ,i want to disable the check box when we select any particilar item from dropdown list.suppose i have five item in dropdown list box(A,B,C,D,E) and two...
6
by: peerraghu | last post by:
hi i am developing website which is a online shoping i want to populate two dropdownlist, in which the first dropdownlist should place the list of a paticular product and the next dropdownlist...
1
by: dcrewhouse | last post by:
Hello-- I have been working with the problem for a while and have yet to find a solution. Does anyone know how to populate a drop down list box based on the selection from another drop-down in a...
6
by: troy_lee | last post by:
I have a continuous form that has one combo box (cbo1) with the selectable values of "Removal" and "Installation". I would like to change another combo box (cbo2) value list based on the selection...
8
by: rmurgia | last post by:
Is there a way to set up a dropdown list and have the description field be displayed as a selection for the user and also be displayed in the input field after the user selects it, but have the value...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.