首页 > 解决方案 > Dynamic Select Box Dependent Using MySQL Database

问题描述

I'm developing a website for comparing prices of cars:

The user should enter: Year Make Model

Database Structre & Example:

id -- year -- make -- model
1 2018 Fiat Ponto
2 2018 Seat Leon
3 2017 Hyundai Veloster

So Now I'm displaying Select boxes but I need it to get the Select Box Value Before submitting form.

              <div>
                <?php
                     $cars = "SELECT distinct year FROM `cars` ";
                      $carsResult = mysqli_query($conn, $cars);
                 ?>
              <select name="year" class="selectStyle">
                   <option disabled selected >Year</option>
                    <?php
                           if(mysqli_num_rows($carsResult) > 0){
                         while($row = mysqli_fetch_assoc($carsResult)){
                                                              ?>
                 <option id="year" value="<?php echo $row['year']; ?>"><?php echo $row['year']; ?></option>

                                                              <?php
                                                          }
                                                      } else {
                                                          ?>
                                                              <option       value="">0 results</option>
                                                          <?php
                                                      }
                                                      ?>
                                  </select>


                <


          <select name="make" class="selectStyle">
          <option disabled selected>Make</option>
          // Here I need to display more

          </select>


              </div>

So on the second Select I need to display only for the selected year: "SELECT make FROM cars WHERE year = {users year}" How can i get the user selected year to the sql query?

So after i can check: "SELECT model From cars WHERE year= {users year} AND model = {users model}"

Please help me, I'm tring forever now!

标签: javascriptphpmysqlajaxhtml

解决方案


推荐阅读