Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7973141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:01:48+00:00 2026-06-04T08:01:48+00:00

I am have three drop down list : Country , State and City. I

  • 0

I am have three drop down list : Country , State and City.

I want to populate state list based on selection in country list and populate city list based on selection in state list.

Here’s how I have attempted this

  1. From the country list, using the onchage event I call java script function.

    <select name="country" id="id1" onchange="onCountryChange(this.value)">
    
  2. Then I send a request to server to get names of states associated with country.

    function onCountryChange(str){
        if (window.XMLHttpRequest){ 
            xmlhttp=new XMLHttpRequest();
        }
        else{
             xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function(){
        if (xmlhttp.readyState==4 && xmlhttp.status==200){
                document.getElementById("statenames").innerHTML=xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET","./view/countrychange.php?countryname="+str,true);
    xmlhttp.send();
     }
    
  3. The I get the values of states using the following code.

    <?php
    $countryname=$_GET["countryname"];
    $connection = pg_connect("host=localhost port=5432 dbname=STAGE2 user=postgres password=jssate");
    if (!$connection){
        echo "<H1>Error in connection to Database</H1>";
        echo "<H2>Please try again</H2>.";
    }
    else{
        $query1="Select distinct(state) from master_table where country='$countryname'";
    $result = pg_query($connection,$query1);
    $numrows = pg_numrows($result);     
    if ($numrows>0){
    echo "State <select name="state" id="stateid">;
    for ($row_index=0;$row_index<$numrows;$row_index++)
    {
        $state_name=pg_result($result,$row_index,0);
        echo "<option value="$state_name\">$state_name</option>";
            }
    echo "</select>";
    }
    
    }
    pg_close($connection);
    ?>
    

3 .Used the following code on my client side to display the result.

    <div id="statenames">
    //Original select list placed here is replaced by the one returned by server.
    </div>

This does work for me and the states are populated based on country name. Now how do I populate the third list. Is the approach I have chosen for displaying the state list correct? As I have no control on the select list of state which I get now.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T08:01:49+00:00Added an answer on June 4, 2026 at 8:01 am

    Create 3 select country, city and state selects in your html page

    • only country select element will have option, state and city will be empty.
    • on change event of country send and ajax request to fetch state, and as the response send only option elements DO NOT WRAP THEM IN A SELECT as you have done
    • Push these options into state’s select element

      document.getElementById(‘state’).innerHTML(state_ajax_response);

    Then create function in your html file that will be called on the state select change and again repeat the procedure for the cities select

    Hope you get it.

    Just send option elements as response.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three drop down list <h:selectOneMenu> , and a <p:dataTable> . I want
I have three cascaded drop down lists. ddlIllnessType, ddlIllnessSubType and ddlInfectiousAgent Initially, only ddlIllnessType
I have three drop down select lists, when a value from one of the
I have the following action method that builds a drop down list which is
I have a drop-down list box and two text fields of id values namely
I have a drop down list and 5 divs along with it.I need to
I have a client who would like to have drop down list added to
I have a three dropdownlists country, city and district with and initial blank selected
I have a drop-down list containing classifications of currencies which can be instantiated as
I have three identical drop down lists that I'm populating using MySQL data. If

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.