Pass value from select to route in laravel

0

I do not know much about web development and I'm kind of lost. I need to pass the id of a movie to a laravel route and I'm not sure how to get that value.

<select class="form-control" style="width: 20%"  onchange="" id="select" name="filme">
  <option value="" selected disabled>Selecione um filme</option>
    @foreach($filmes as $f)
      <option value="{{$f->id}}">{{$f->nome}}</option>
    @endforeach
</select>
<?php echo $var = pegaId(); ?>
<br><br>
  <button type="submit" class="w3-btn-block" onclick="window.location='/filmes/{{$var}}'">Ver eventos</button>

<script type="text/javascript">
  function pegaId(){
    return document.getElementById('select');
  }
</script>

I've read that there is no way to assign the return of a JS function to a php variable because the JavaScript runs on the client side and PHP on the server side, so my variable does not recognize the function return at that time. Is that right?

When I call the function pegaId() of the error

  

Call to undefined function pegaId ()

I believe I have an easier way to do this, like calling the JS function in my url but I'm not getting it: (

    
asked by anonymous 11.03.2017 / 15:30

1 answer

1

I think what you want is something like this:

View:

<form class="form-horizontal" role="form" method="POST" action="{{ route('selecionar.filme') }}">
{{csrf_field() }}

<select class="form-control" style="width: 20%"  onchange="" id="select" name="filme">
  <option value="" selected disabled>Selecione um filme</option>
    @foreach($filmes as $f)
      <option value="{{$f->id}}">{{$f->nome}}</option>
    @endforeach
</select>

<br><br>
  <button type="submit" class="w3-btn-block">Ver eventos</button>
</form>

Route:

Route::post('selecionarFilme', ["as" => 'selecionar.filme', 'uses' => "FilmeController@store"]);

Controller:

public function store(Request $request)
{
        $selecao = $request->get('select');

        return redirect('filmes/'.$selecao);
}
    
11.03.2017 / 15:53