0

I have two <select>, Categories and Subcategories. I want when i click an option/value from Categories then load & update the second <select> from mySQL. Subcategories values are in mySQL.

(1) Category Select

<select class="form-control" onchange="check()"  id="category" name="category">
    <option  value="cafe">Café</option>
    <option  value="bars-clubs">Bars-Clubs</option>
    <option  value="restaurants">Restaurants</option>
    <option  value="shopping">Shopping</option>
    <option  value="services">Services</option>
    <option  value="sducation">Education</option>
</select>

(2) Subcategory Select

<select class="form-control " id="SubCategory" name="subCategory">
    <?php include "server.php";

        $qyerry = "select * from " + [[FROM CATEGORY VALUE]]
        $result = mysql_query($qyerry);
        while($row = mysql_fetch_array($result)){
            echo "<option value=''>" . $row{'name'} . "</option>";
        }
    ?>
</select>

Here is my Javascript Code to get the value of onchange value option

<script>
   function check(){
      var x = document.getElementById("category").value;
   }                                
</script>
Rafsonic
  • 17
  • 3

1 Answers1

0

To implement cascading comboboxes, you need to use one of the following methods:

  • Submit your form when changing the parent combobox.

    this means that, on your first select, you'll need to have something like this :<select class="form-control" onchange="location.replace('current?action=CategorySelectionChanged')". And in your server side prepare a query and passe its results to client side.

  • Use an XmlHttpRequest to get the child's content and parse it via Javascript.

    Again, you'll need to fire a change event, that will call the server.I assume that you are using jQuery, so the writing will be via Ajax call.

    Please take a look on this link.

Incepter
  • 2,711
  • 15
  • 33
  • Okay man.. thanks a lot!! – Rafsonic Nov 15 '16 at 13:17
  • you re welcome, don't hesitate to ask if you re blocked! – Incepter Nov 15 '16 at 13:18
  • Finally i did it. All Subcategories appearing from mySQL. But there is a problem. When i trying to print the value of each – Rafsonic Nov 23 '16 at 23:32
  • i am not sure to understand your problem now, do you have a problem with the click event on an option ? – Incepter Nov 24 '16 at 10:02