Select related to another select, pulling bank data

0

I need to create a form that pulls the sql bank options. So far so good, I've done a while inside a query in php;

echo '<SELECT NAME = "setor">';
    while ( $temp = $setores->fetch_assoc() ) {
        echo '<OPTION>'.$temp['nome'];  
    } 
echo '</SELECT>';

But I need a second select only to present the information according to the option selected in the first select.

So for example, if in this select the user select '' movies '', the next select should display the movies in the database, if he selects '' series '', he returns the recorded series, and so on.

What's the simplest way I could do this?

    
asked by anonymous 09.10.2017 / 15:14

1 answer

-1

In your select onChange event, create a method and search in ajax to popular the second select.

Something like this:

$('.select1').on('change', function(){

    var id = $(this).val();

    $.ajax({
        url: 'url',
        data: {
            id: id
        },
        type: 'GET',
        dataType: 'json',
        success: function(data){

            var html = '';

            $.each(data, function(){
                html += "<option value='"+this.id+"'>"+this.texto+"</option>";
            });

            $('.select2').html(html);
        }
    })

})

Remembering that in PHP you should return a json to JS:

  

echo json_encode ($ arrReturn);

    
09.10.2017 / 15:58