Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

272
Vistas
how do I pass a variable from js file to laravel controller to make a query based on the variable taken from an element clicked

this is code in laravel controller

$data = table::where('field',$var)->get();

this is the js file im trying to receive the variable from to pass it to the query

function clicked(var)
{
    console.log(var);
}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

The Front end will need to send data to the backend via a HTTP request. This can be done via AJAX. There are good libraries that you can use to do this such as AXIOS if you are using Laravel with Vue.JS. Here is an example of how to use it:

axios.post('/URL_HERE', {
    firstName: 'Fred',
    lastName: 'Flintstone'
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });
about 4 years ago · Juan Pablo Isaza Denunciar

0

This is the alternative method I have chosen to go with. this is the js file:

var region = "regionInput";
$.ajaxSetup({
        headers: {"X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr('content')},
    });
    $.ajax({
            method: "POST",
            url: "/test-route",
            //dataType:"jsonp",
           // dataType:"String",
            data:{regionName: region},
            success: function(result){
                console.log(result);
            }
        });

this is the web.php file

Route::post('/test-route',[App\Http\Controllers\regionsController::class, 'testMethod']);

this is the function in the controller

public function testMethod(Request $request){
  
  return response()->json($request->input("regionName"));
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda