Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

274
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!