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 4077760
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:35:25+00:00 2026-05-20T17:35:25+00:00

I have 2 dropdowns on my HTML page : The first drop down contains

  • 0

I have 2 dropdowns on my HTML page : The first drop down contains the database column names based on which the the second dropdown would be populated i.e.

I have a single table with fields: <Student Name, Degree, City> and following would be the entries;

1. "A", "BS", "New York"
2. "B", "BS", "Chicago"
3. "C", "MS", "Boston"
4. "D", "MS", "New York"

So my first dropdown would contain the column names i.e. “Degree” and “City”.

If I select “Degree”, the 2nd dropdown should populate “BS” and “MS” and if I select “City”, the 2nd dropdown should select “New York”, “Boston” and “Chicago”.

How can I go about with the implementation?

[Adding my code]:

the changeSecond(first) method remains exactly the same as you suggested

<body>
   <form method="POST" action="" name="mainForm">
      <table>
         <tr>
            <td> Filter by: </td>
            <td>
            <div id="first">
               <select onChange="changeSecond(this.value)">
                  <option value="1">All</option>
                  <option value="2">Degree</option>
                  <option value="3">City</option>
               </select>
            </td>
         </tr>
         <tr>
            <td>&nbsp</td>
            <td>
               <div id="second">
                  <select name="val">
                     <option value=""></option>
                  </select>
               </div>
            </td>
         </tr>
      </table>
   </form>
</body>

And this is the second_script.php as you suggested:

<?
   $link = mysql_connect("localhost", "root", "");

   if (!$link)
   {
      die('Could not connect: ' . mysql_error());
   }
   if (mysql_select_db("myDatabase", $link))
   {
      $first=mysql_real_escape_string($_REQUEST["first"]);
      $query="SELECT ".$first." FROM myTable GROUP BY ".$first;
      $data=mysql_query($query);

      echo "<select id=\"second\">";
      while($row=mysql_fetch_row($data))
      {
         echo "<option value=\"".$row[0]."\">".$row[0]."</option>";
      }
      echo "</select>";
   }
   echo mysql_error();
?>
  • 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-05-20T17:35:26+00:00Added an answer on May 20, 2026 at 5:35 pm

    If you want a more dynamic solution (that will accommodate changes to the background DB) you can do something like this on your page:

    <script>
            function changeSecond(first){
            var xmlhttp;
            if (window.XMLHttpRequest)
              {// code for IE7+, Firefox, Chrome, Opera, Safari
              xmlhttp=new XMLHttpRequest();
              }
            else
              {// code for IE6, IE5
              xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
              }
            xmlhttp.onreadystatechange=function()
            {
              if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                var res=xmlhttp.responseText;
                document.getElementById("second").innerHTML=res;
                }
              }
            xmlhttp.open("GET","second_script.php?first="+first,true);
            xmlhttp.send();
            }
            </script>
    ...
    <select onChange="changeSecond(this.value)">
    <option value="Degree">Degree</option>
    <option value="City">City</option>
    </select>
    <div id="second"><select><option value=""></option></select></div>
    

    and then a script similar to:

    <?php
    //database connection
    $first=mysql_real_escape_string($_REQUEST["first"]);
    $query="SELECT ".$first." FROM tablename GROUP BY ".$first;
    $data=mysql_query($query);
    echo "<select>";
    while($row=mysql_fetch_row($data)){
    echo "<option value=\"".$row[0]."\">".$row[0]."</option>";
    }
    echo "</select>";
    ?>
    

    I guess for real flexibility you’d also want to dynamically populate that first one using mysql_field_name in another script similar to above

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

Sidebar

Related Questions

I have two drop down menus, one of which I am trying to replace
I have a HTML page with 3 dropdowns for the month, day and year
I have a custom built ajax [div] based dynamic dropdown. I have an [input]
I have a dropdown menu with this HTML: <ul id=dropdownmenu class=dropdown maintopmenu> <li class=topmenu
I am using HTML and JQuery. I have got below HTML for the dropdown.
I have a web form that has multiple ListBoxes, TextBoxes, DropDowns. If I put
I have dropdown list control in one of my application and when I add
I have a dropdown list that stores name/value pairs. The dropdown appears in each
I have a dropdown box and a literal tag inside an Update Panel. On
I have a dropdown, I have a datasource, I have AutoPostBack set to true

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.