In a html if i press india i should get its states, and if i press australia i should get its states
-It should be in drop down list only
can some one please help me out

Recommended Answers

All 11 Replies

i think you need to store the states in a DB using the country as primary key.

then add an event on the onClick of your button.

On this onClick event, you will populate your dropdown list according to the records you have selected from the database.

to--Eric Cute


Do u have any sample code

Nope (",) try to google some sample database access codes. If you had that working, try researching for the button onClick event.

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
         <tr>
<td><h3>Country</h3></td>
<td><select name="country" id="country"><option value="0"><h3>select</option></h3>
<option value="i">India</option>
<option value="a">Australia</option>
<option value="p">Pakistan</option>
<option value="u">USA</option>
<option value="s">South Africa</option>
<option value="sr">SriLanka</option>
</select>
</td>
</tr>
 <tr>
<td><h3>State</h3></td>
<td><select name="state" id="state"><option value="0"><h3>select</option></h3>
<option value="iap">Andhra Pradesh</option>
<option value="im">Maharastra</option>
<option value="imp">Madhya Pradesh</option>
<option value="ia">Assam</option>
<option value="av">Victoria</option>
<option value="aq">Queensland</option>
<option value="as">South Australia</option>
<option value="aw">Western Australia</option>
<option value="ps">Sindh</option>
<option value="pp">Punjab</option>
<option value="pk">Khairpur</option>
<option value="pm">Mirpur</option>
<option value="ua">Alabama</option>
<option value="ua">Alaska</option>
<option value="uc">California</option>
<option value="uw">Washington</option>
<option value="sa">Johannesburg</option>
<option value="sc">Cape Town</option>
<option value="sp">Port Elizabeth</option>
<option value="sd">Durban</option>
<option value="srd">Dambulla</option>
<option value="srg">Galle</option>
<option value="srk">Kandy</option>
<option value="src">Colombo</option>
</select>
</td>
</tr>
    </body>
</html>

this is my actuall code if i press india it is displaying the whole states i actually want it to display 4 states which are in india

Can anyone sort it out...

Next time, please wrap your codes inside the CODE-tag.

if that is your html, then YES it will display all States whichever you press on your Country option. The key here is you need to create your States dropdown programmatically, that means you do not just type the option values.

My suggestion.

i. Store your states in a db or text file. suggested form is,

    value state name

example:

     iap andra pradesh
     im marahastra
         ........

ii. When user already selected a country, trigger an event wherein you will read the text file and include only those states from the selected country.

Example:

country option = india     value = "i"

when you read your text file, you can do check the value since all india state values starts with "i";

if (readStateValue.equals(selectedCountryValue)) {
\\ then include it in your result, else dont
}

read on bufferedReader and java.io for more info on those.

after the event, redirect on your original JSP (now showing the staes).

I am new to java coding ..so can u please write the code...
I have created tables

If you are new to java and don't know how to read from database then why do go and do jsp pages.

First learn java and then do jsp
First learn how to read from database and then do jsp.

And even if you knew all these you still need to lean first html and submitting form and getting the request before doing what you want.

The point is that you need to do some studying and if you don't understand the advices of the previous posts (Eric Cute's for example) then giving you the code will not solve anything.

Better start by searching examples on how to read data from the DB and then try to display them.

<%--
    Document   : cout
    Created on : Jan 18, 2011, 9:41:20 PM
    Author     : Prashanth.yatakeeri
--%>

<%@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 language="java" import="java.sql.*"%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>

<tr>
<td><h3>Country</h3></td>
<td><select name="country" id="country"><option value="0"><h3>select</option></h3>

<%
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con=null;
ResultSet rst=null;
Statement stmt=null;

try{
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/Prashanth","root","");
stmt=con.createStatement();
rst=stmt.executeQuery("select countryname from country ");
while(rst.next()){


%>

<option value="<%=rst.getString("countryname")%>"><%=rst.getString("countryname")%></option>
</select>
</td>
</tr>


<% }


rst.close();
stmt.close();
con.close();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
%>
   </tbody>
</table>
</center>
</div>


</body>

</html>

Better. Now take that code and put it in a separate class. That class will have a method that returns a List of those countries:

public ArrayList getCountries() {
  // your code here
}

Then call that method from the jsp. Don't put connection code in the jsp:

<%
ArrayList countries = yourClass.getCountries();
%>

<select name="countries">

<%
for (....) {
%>
   <option value="<%=countries.get(i)%>" ><%=countries.get(i)%>
   </option>
<%
}
%>


</select>

Also the </select> needs to be outside of the loop

Thanks all i got it...now i want country and state to be displayed from the database into jsp page

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.