2

I am using two selects, the one as the category and the second one as subcategory. I wish to make the second select menu to be determined by what option was chosen in the second select menu.

Here is the html/php code.

<select name="category" id="category">
  <?php 
$options = array("Stationary", "Bag", "Paper");
      foreach ($options as $option) {
    echo '<option value"' . $option . '"';
     if(in_array($option, $category)){
       echo " Selected";
     }
       echo ">" . ucfirst($option) . "</option>";
  }
  ?>
</select>

<select name="subcategory" id="subcategory">
    <?php
    $options = array("Pencils", "Ruler", "Pens");
      foreach ($options as $option) {
        echo '<option value"' . $option . '"';
          if(in_array($option, $category)){
            echo " selected";
          }
        echo ">" . ucfirst($option) . "</option>";
      }     
    ?>
     <?php
    $options = array("Small", "Large", "Paper");
      foreach ($options as $option) {
        echo '<option value"' . $option . '"';
      if(in_array($option, $category)){
        echo " selected";
      }
    echo ">" . ucfirst($option) . "</option>";
     }
    ?>
  <?php
    $options = array("Notepad", "Plain ", "Colour");
      foreach ($options as $option) {
        echo '<option value"' . $option . '"';
      if(in_array($option, $category)){
        echo " selected";
      }
    echo ">" . ucfirst($option) . "</option>";
     }
     ?>           
</select>

I want it so when and you select one option from category it will it show the correct options to be shown in the <select>.

I'm not sure if you can do this, but if anyone has an idea or know how to do this your help would be helpful.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339

1 Answers1

0

If I understand your question right what you want to do is to select the sub category depending on the category.

1st load all the categories into the category select box.

2nd when the category option is selected retrive the value from select box(category_id) and make an AJAX call to get the sub categories for the selected category.

3rd populate them to the sub category select box.

Updated answer as requested

jQuery Ajax POST example with PHP

If you have any issues let me know

Community
  • 1
  • 1
Techie
  • 44,706
  • 42
  • 157
  • 243