Fill a select by clicking the previous select

Asked

Viewed 45 times

0

My php code

public function getCadastro(Request $request)
{

    $redes = Rede::lists('nome','id');

    $municipios = Municipio::select('nome', 'id')->pluck('nome','id')->all();
    $municipios_id = $request->has('municipio_id') ? $request->get('municipio_id') : null;

    $regionais = Regional::select('nome', 'id')
                            ->join('municipios','municipios.regional_id','=','regionais.id')
                            ->where('municipios.id','=', $municipios_id);



    return view('auth.cadastro',compact('redes', 'municipios', 'municipios_id', 'regionais'));
}

Here is my view

<div class="row">
        <div class="col-sm-4">
    {!! Form::label('municipio_id', 'Municipio*:') !!} <br/>
    {!! Form::select('municipio_id',$municipios,$municipios_id,['class'=> 'form-control']) !!}
    {!! $errors->first('municipio_id', '<span class="text-danger">:message</span> ') !!}

  </div>
    <div class="col-sm-4">
        {!! Form::label('regional_id', 'Regional*:') !!} <br/>
        {!! Form::select('regional_id',$regionais,null,['class'=> 'form-control']) !!}
        {!! $errors->first('regional_id', '<span class="text-danger">:message</span> ') !!}

    </div>

I’m new in PHP if you can help me.

  • 1

    What do you want to do basically? Be more detailed please!

  • You need to work with Ajax from what I could understand basically.

  • vlw, I’ll search here. Thank you

1 answer

0

It can be solved using Jquery Ajax. It would look like this:

 <script>
    $('#municipio_id').change(function () {

        $.get('/rota_para_regionais/', {
                municipio_id: $('#municipio_id').val()
            },
            function (resp) {
                $('#regional_id').html('');
                for (i = 0; i < resp.length; i++) {
                    $('#regional_id').append('<option value="'+resp[i].id+'">'+resp[i].nome+'</option>');
                }
            }
        );

    });
</script>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.