简体   繁体   中英

Filter SELECT box from previous SELECT box PHP - SQL Server

I have a couple of SELECT boxes that are pulling from the database, how can I get the option that is selected in SELECT box 1 to filter the SQL in the SELECT box 2?

EG SELECT 1 - Make (Audi, BMW) SELECT 2 - Model (A1,A3, 1 Series, 3 Series)

I want to show that if I pick Audi from SELECT 1 that it will use Audi to fill the WHERE clause in my SQL to filter for the SELECT 2

<label>Manufacturer</label>
<select class="select2_category form-control" data-placeholder="Choose a Category" tabindex="1" id="make" name="make" >
    <option value=""></option>
    <?php $sqlmake = odbc_exec($cnn, "SELECT DISTINCT Manufacturer FROM lkup.alldata ORDER BY Manufacturer ");
            while($manurs = odbc_fetch_array($sqlmake)) {                                                                        
                echo '<option value="'. $manurs['Manufacturer'] .'">'. $manurs['Manufacturer'] .'</option>';
        }
    ?>
</select>

From the above I've been working on this below, which returns no errors but also when you click on the SELECT returns no values in the SELECT.

Where have I gone astray?

My SELECT

<select class="select2_category form-control" data-placeholder="Choose a Category" tabindex="1" id="model" name="model">
<option value=""></option>                                                                
</select>
<script>
$(function() {
    $('.make').change(function() {
    var select = $('.model').empty();
        $.get('assets/configs/script.php', {model: $(this).val()}, function(result) {
            $.each(result, function(i, item) {
                $('<option value="' + item.value + '">' + item.name + '</option>').
                    appendTo(select);
            });
        });
    });
    });
   </script>

The script.php

<?php
session_start();
date_default_timezone_set("Europe/London");
error_reporting(0);    

include 'config.php'; //my db settings

if (isset($_GET['model'])) {      
    $model = addslashes($_GET['model']);
    $sqlmodel = odbc_exec($cnn, "SELECT DISTINCT ModelName FROM lkup.MyTable WHERE ModelName IS NOT NULL AND Make = $model ORDER BY ModelName ");
    $modelrs = array();
    while ($row = $sqlmodel->fetch_assoc()) {
        $modelrs[] = array(
            'ModelName' => $modelrs['ModelName']
        );
    }
    echo json_encode($modelrs);
}?> 

If you are familiar with javascript or jQuery you could run another php echo statement to create the other select options with all the possibilities, and then with jQuery hide or show the appropriate options.

    $('select[name=first_select]').on('change', function(){
    var make = $(this).val();
        $.each($('select[name=second_select] option'), function(){
          if($(this).prop('class') != make){
            $(this).hide();
          }
      }
    });

After a lot of problems with this I found this link that worked a treat. I think my main problem is that I'm using PHP & MS SQL Server, which makes life difficult.

This I found as my solution

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM