capture more than 1 value with ajax and laravel 5

1

I have in my view two selections with data, which I want to capture their values. I can do it with a select and the $.get method, but I do not know how to do it so I can capture two data and take it to a controller.

I do not know if my problem is understood, but I leave what I have to capture a single data.

Route:

Route::get('imp/{id}', 'VentasController@getIva');

Controller:

public function getIva(Request $request, $id)
{

    //$test = M_DCVT::where('id' , '=' , $id)->select('mimpcd_id')->first();
    //$test = $test->mimpcd_id;


    if($request->ajax()){

        //$imp = M_IMPT::Iva($id, foo);
        //return response()->json($imp->mimpts);


    }   

}

Script:

$("#mdccod_id").change(function(event){

    var select = $('#msocod_id option:selected').val(); 

    $.get("/imp/"+event.target.value+"",function(response){          
      $("#hidden").val(response)
    });
});
    
asked by Andrés Gómez Vega 12.07.2016 в 02:56
source

1 answer

2

Assuming then that we have two select:

<select id="mdccod_id">
  <option value="1">valor1</option>
  <option value="2">valor2</option>
</select>

<select id="msocod_id">
  <option value="1">valor1</option>
  <option value="2">valor2</option>
</select>

And that we are going to capture the change event (according to what you answer in the comments) in only one of the two select, for which we are going to keep the get that is being used:

$("#mdccod_id").change(function(event){

  var select = $('#mdccod_id option:selected').val(); 
  var select2 = $('#msocod_id option:selected').val(); 

  $.get("/imp/" + select + "/" + select2 ,function(response){          
    $("#hidden").val(response)
  });
});

The previous one is a quite "manual" solution which could be improved depending on how you have projected the application and also put some default value in case there is nothing in the values you take from the select.

Next we modify the route in Laravel so that it can receive the two parameters, which we will make obligatory:

Route::get('imp/{id}/{valor2}', 'VentasController@getIva');

Finally, we simply add the additional parameter to the controller method:

public function getIva(Request $request, $id, $valor2)
{

    if($request->ajax()){
        // hacer lo que quieras con $id
        // hacer lo que quieras con $valor2
    }   

}
    
answered by 12.07.2016 / 06:00
source