Retrieve the value of a select from a form

1

I have a form with a single select where it is automatically filled with a mysql table, the detail is that I want that when someone selects an option and clicks on the send button, I can recover that value to be able to make a consult and show the data, it is not like a dependent select, but in my select I have the data of some races, if I select one later I appear the data of the subjects of that race along with other specifications.

The problem is that I do not know how to recover the value sent by the select.

My code is this:

{!!Form::open(['route' => 'plan.mostrar','method' => 'POST'])!!}
    <div class="form-group">
        {!!Form::label('Seleccione RVOE:')!!}
        <div class="input-group">
            <div class="input-group-addon">
                <i class="fa fa-graduation-cap"></i>
            </div>

            {!!Form::select('rvoe',$rvoe,null,['id' => 'rvoe','class' => 'form-control'])!!}

            </div>
    </div>

    {!!Form::submit('Enviar',['name' => 'guardar','id' => 'guardar','content' => '<span>Enviar</span>','class' => 'btn btn-success btn-lg pull-right'])!!}

{!!Form::close()!!}
    
asked by David Martinez 05.07.2016 в 03:22
source

2 answers

0

In your controller you can inject (using the injection of dependencies) a request, which is in this case the POST made when sending the form.

routes.php

Route::post('carrera', 'PlanController@mostrar')->name('plan.mostrar');

Controller:

namespace app\Http\Controllers;

use Illuminate\Http\Request;

class PlanController extends Controller
{

    public function mostrar(Request $request)
    {
        // selecciona directamente el valor seleccionado del select
        $carrera = $request->input('rvoe');

        // puedes obtener todos los valores enviados en el request en forma de array
        // $valores = $request->all();
    }

}

You can also replace the generic $request we are using with a custom one that includes validations of the form fields and even permission verification if your application needs it.

You can see all the information related to this answer in the official Laravel documentation: Requests and Validations

    
answered by 05.07.2016 в 03:43
0

The option that I look at is the following.

<select value = "" name="valor">
 @if($valor != null)
    <option value="{{$valor}}" selected="" > Carrera1 </option>
 @endif
</select>

in the controller

public function mostrar(Request $request){
  $valor = $request->get("valor");
  return redirect->route("formulario",[valor = $valor]);
}

What I do is:

  • I define the value in the template, where I define the @if where what I say is if this value exists that selects it in the template as selected .
  • In the Controller, I capture the response of the form and return it through the router, where you have to create a custom route:

    route :: get ("form / {race}") - > name ("form");

  • What would be missing is to define in js, if it exists or to complete it with the subjects related to the race.

        
    answered by 05.06.2017 в 21:19