Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Populate multiple select box with jquery and laravel

I am using laravel and i need to populate multiple select according to what user choose from a database. I have 2 select box, one choose category, then once i choose the category, the second select box is populated with product. Once the second select is populate with product i need to fetch from database all the description of that product and show in a form so it can be modified.

I did a Route:

 Route::get('api/dropdown', function(){
            $input =Input::get('option');

                $prodotti= DB::table('prod')                
                    ->join('cat','cat.id_prod','=','prod.id')
                    ->where('cat.id','=',$input)
                    ->select('prod.id as idprod','prod.nome as nomeprod')
                    ->lists('nomeprod','idprod');


            return Response::json($prodotti);
            });

This should send a response

{{ Form::open(array('url' => 'admin/create/mod', 'files'=> true)) }}
        <span>
        {{ Form::select('categorie',  $categorie,'default', array('id'=> 'a')) }}
            <br/>

        {{ Form::select('a',array('a'=> 'scegli'),'default', array('id'=> 'b')) }}

        Scegli il nome del prodotto

        {{Form::text('nome')}}
        <br/>
        {{Form::label('*Descrizione in Italiano')}}
        <br/>
        {{Form::textarea('descrizioneit','',array('id'=>'descrizioneit'))}}
        {{Form::textarea('descrizioneit','',array('id'=>'previewit', 'readonly'=>'readonly', 'onfocus'=>'this.blur();'))}}
        <br/>
        {{Form::label('*Descrizione in Inglese')}}
        <br/>
        {{Form::textarea('descrizioneen','',array('id'=>'descrizioneen'))}}
        {{Form::textarea('descrizioneen','',array('id'=>'previewen', 'readonly'=>'readonly', 'onfocus'=>'this.blur();'))}}
        <br/>

{{Form::close()}}

This is my javascript

jQuery(document).ready(function($){
    $('#a').change(function(){
            $.get("{{ URL::to('api/dropdown')}}", 
                { option: $(this).val() }, 
                function(data) {
                    var model = $('#b');
                    model.empty();

                    $.each(data, function(element) {
                        model.html(element);
                    });
                });
        });
    });

Then i should do another api to populate the textbox with the product description i have chosen?

I would follow this idea but even the firt part of this doesn't work. It seems that my javascript code is ignored.

like image 904
user3384514 Avatar asked Nov 10 '22 09:11

user3384514


1 Answers

The problem lies in how you are filling out your select.

jQuery(document).ready(function($){
    $('#a').change(function(){
        $.get("{{ URL::to('api/dropdown')}}", 
            { option: $(this).val() }, 
            function(data) {
                var model = $('#b');
                model.empty();

                $.each(data, function(index, value) {     
                     model.append($("<option />").val(index).text(value)); });
                });
          });
    });
});
like image 56
user1669496 Avatar answered Nov 14 '22 22:11

user1669496