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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:23:18+00:00 2026-06-08T18:23:18+00:00

I am building a website to learn coding and am trying to build a

  • 0

I am building a website to learn coding and am trying to build a tool where a user clicks on a select/dropdown that contains some category names pulled from database cat and then another select will appear with subcategory names pulled from database subcat. This is almost exactly like Yelp’s (go down to the categories) like Yelp’s (go down to the categories).

I also made a diagram:

enter image description here

I already have a category dropdown that is pulling from cat database:

<p><b>Category:</b><br />
 <?php
  $query="SELECT id,cat FROM cat";
  $result = mysql_query ($query);
  echo"<select name='cselect3' class='e1'><option value='0'>Please Select A       Category</option>";
  // printing the list box select command
  while($catinfo=mysql_fetch_array($result)){//Array or records stored in $nt
  echo "<option value=\"".htmlspecialchars($catinfo['cat'])."\">".$catinfo['cat']."    </option>";

  }

echo"</select>";
?>

And I have a subcat that is pulling from subcat database:

<p><b>Subcat1:</b><br />
<?php
  $query="SELECT id,subcat FROM subcat";
  $result = mysql_query ($query);
  echo"<select name='sselect1' class='e1'><option value='0'>Please Select A Category</option>";
  // printing the list box select command
  while($catinfo=mysql_fetch_array($result)){//Array or records stored in $nt
      echo "<option value=\"".htmlspecialchars($catinfo['subcat'])."\">".$catinfo['subcat']."</option>";

  }

 echo"</select>";
?>

How do I make a subcategory dropdown based on what the user clicks on category and make it automatically appear?
Thanks so much for any and all help!

  • 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-08T18:23:19+00:00Added an answer on June 8, 2026 at 6:23 pm

    I would just make put the variables in javascript with php and then use javascript functions.. no jquery or AJAX needed.

    However you need to have a foreign key for subcategories no matter what.. ie – For every record in subcat table you need to give it a catid so for referencing…

    <?php
      $db = new mysqli('localhost','user','password','dbname');//set your database handler
      $query = "SELECT id,cat FROM cat";
      $result = $db->query($query);
    
      while($row = $result->fetch_assoc()){
        $categories[] = array("id" => $row['id'], "val" => $row['cat']);
      }
    
      $query = "SELECT id, catid, subcat FROM subcat";
      $result = $db->query($query);
    
      while($row = $result->fetch_assoc()){
        $subcats[$row['catid']][] = array("id" => $row['id'], "val" => $row['subcat']);
      }
    
      $jsonCats = json_encode($categories);
      $jsonSubCats = json_encode($subcats);
    
    
    ?>
    
    <!docytpe html>
    <html>
    
      <head>
        <script type='text/javascript'>
          <?php
            echo "var categories = $jsonCats; \n";
            echo "var subcats = $jsonSubCats; \n";
          ?>
          function loadCategories(){
            var select = document.getElementById("categoriesSelect");
            select.onchange = updateSubCats;
            for(var i = 0; i < categories.length; i++){
              select.options[i] = new Option(categories[i].val,categories[i].id);          
            }
          }
          function updateSubCats(){
            var catSelect = this;
            var catid = this.value;
            var subcatSelect = document.getElementById("subcatsSelect");
            subcatSelect.options.length = 0; //delete all options if any present
            for(var i = 0; i < subcats[catid].length; i++){
              subcatSelect.options[i] = new Option(subcats[catid][i].val,subcats[catid][i].id);
            }
          }
        </script>
    
      </head>
    
      <body onload='loadCategories()'>
        <select id='categoriesSelect'>
        </select>
    
        <select id='subcatsSelect'>
        </select>
      </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building a website to learn coding, and am trying to build a Claim
Building a new website at work and I'm trying to learn some TDD so
I'm trying to learn MVC by building a full-featured website. I'm a little stuck
I'm building a website to learn coding. I have a claim.php which is a
I am trying to learn Spring framework by building simple website, now I have
Building a website that has English & Japanese speaking users, with the Japanese users
I am building a website templating system where multiple user can create their own
I am building a website to capture data. I have many spreadsheets that are
I am building a website that would serve dynamic content. All communication between server/browser
I'm building a website with ASP.NET MVC3. When a user signs in, I pull

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.