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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T13:29:35+00:00 2026-06-08T13:29:35+00:00

I’m making a website to learn coding and am making 2 select dropdowns that

  • 0

I’m making a website to learn coding and am making 2 select dropdowns that will have 1 be populated from the database table -> cat. The other select dropdown will be populated from table -> subcat. The database for cat and subcat look like this:

Table Cat

id (int 15) || cat(varchar 75) || number (int 3)

Table Subcat

id (int 15) || subcat(varchar 75) || catnumber (int 3)

Every subcat row has a catnumber that corresponds with the row in cat -> number. So for example if we have Restaurants which is a row in Cat that has a number of 2, then if we also have American Food, and Chinese Food which have catnumber’s of 2, then they are corresponding.

Here’s my code for pulling out of the database for all 3 of my cat dropdowns.

<p><b>Cat1:</b><br />
<?php
  $query="SELECT id,cat FROM cat";
  $result = mysql_query ($query);
  echo"<select name='cselect1' 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>";
?>

<!-- Next CAT -->

<p><b>Cat2:</b><br />
<?php
  $query="SELECT id,cat FROM cat";
  $result = mysql_query ($query);
  echo"<select name='cselect2' 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>";
?>

<!-- Next CAT -->

<p><b>Cat3:</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 here’s my code for pulling out of the database for all 3 of my subcat dropdowns

<p><b>Subcat1:</b><br />
<?php
  $query="SELECT * FROM subcat WHERE catnumber='1' ";
  $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>";
?>
<p><b>Subcat2:</b><br />
<?php
  $query="SELECT id,subcat FROM subcat WHERE catnumber='1' ";
  $result = mysql_query ($query);
  echo"<select name='sselect2' 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>";
?> 
<p><b>Subcat3:</b><br />
<?php
  $query="SELECT id,subcat FROM subcat WHERE catnumber='1' ";
  $result = mysql_query ($query);
  echo"<select name='sselect3' 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>";
?>

So right now all of the subcat selects are trying to get all the subcategories for the cat with a number of 1 ( which is Restaurants ). How do I get whatever is selected on the cat select to make (without refreshing the page) the corresponding subcat select display the corresponding subcat list for the number of the cat?

^ Very sorry for doing a terrible job explaining it. In the end I basically want it like Yelps -> here

Thanks for 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-08T13:29:36+00:00Added an answer on June 8, 2026 at 1:29 pm

    You need to make an ajax call to query the database in the background, then return the results (as HTML) and populate your subcategory select.

    <select name="cat" id="cat">...</select>
    <select name="subCat" id="subCat">...</select>
    
    <script language="javascript">
        $('#cat').change(function() {
            $.post('getSubcat.php', {
               cat: $(this).val()
            }, function(data) {
                $("#subCat").html(data);
            }
        });
    </script>
    

    Make sure your getSubcat.php script echoes the results as

    <option value='whatever'>Label</option>
    <option value='whatever2'>Label2</option> ...
    

    getSubcat.php would be a simple query / output script:

    <?php
    $category = $_POST['cat'];
    
    $query = "SELECT * FROM subcategories WHERE parent_category = " . $category;
    $result = mysql_query($query);
    
    echo "<option value='0'>Select a subcategory</option>";
    while($row = mysql_fetch_array($result)) {
        echo "<option value='" . $row['id'] . "'>" . $row['subcategory_name'] . "</option>";
    }
    

    NOTE: This utilizes jQuery – make sure your page includes the jQuery libraries

    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a view passing on information from a database: def serve_article(request, id): served_article
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I need a function that will clean a strings' special characters. I do NOT
I have a reasonable size flat file database of text documents mostly saved in
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have two tables with like below codes: Table: Accounts id | username |
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

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.