How to pass javascript variable to laravel route on ajax call

Updated at 22-Nov-2022, By samar

How to pass javascript variable to laravel route on ajax call

Good day, guys. In this post, we’ll look at how to solve the "How to pass javascript variable to laravel route on ajax call" programming puzzle.

You can pass javascript variable value to laravel route on ajax call. You have to use a placeholder to generate the URL and after that replace value by using replace method and pass the value to url parameters of ajax method.
  • Pass javascript variable value to laravel route on ajax call

    <script> 
    var id = $('#recordID').val();
    var url = '{{ route("deleteRecord", ":id") }}';
    url = url.replace(':id', id);
    //Call ajax
    $.ajax({
        type : "POST",
        url : url,
        success:function(response){
            console.log(response);
        }
    });
    </script>
    <!-- Laravel route definition and generated URL -->
    Route::post('/delete-record/{id}', [RecordController::class, 'delete'])->name('deleteRecord');
    <!-- #Generated URL -->
    http://localhost:8000/delete-record/1
    

    You can pass javascript value to laravel route using replace method on url variable and assign to url parameter of ajax method.

  • How to pass javascript variable to Laravel Named Routing

    In my opinion the simplest way is by passing the empty string in place of route paramtere then concatenating javascript variable with blade string as follows.

    var url = "{{route('admin.stocks.edit', '')}}"+"/"+stock.id;

  • How to pass id in ajax url in laravel?

    There are many methods to get the post parameters in the controller function. Use id as a wild card parameter in your route as Route::post('approve/{id}', '[email protected]');.

     $.ajax({
         type: 'post',
         url: 'approve/'+post_id,
    
  • How pass data from route to controller with ajax in Laravel?

    First of all register a route for your URL of AJAX.

    Add you header in the HTML for token: CSRF Protection.

    Write your AJAX js script.

    Call Laravel route with ajax request method GET or POST.

  • How to pass JS variable to route with window.location.href?

    var id = $('#recordID').val();

    var url = '{{ route("deleteRecord", ":id") }}';

    url = url.replace(':id', id);

    window.location.href = url;

  • How to use Laravel variable in Javascript?

    <script type="text/javascript">
        var name = "{{ $name }}";
        console.log(name);
      	var name = "<?php echo $name ?>";
        console.log(name);
    </script>
    
  • How to redirect to route with Javascript in Laravel?

    You can use document.location.href method in javascript to call the route in javascipt.

    function confirmDelete(id){
    	let url = "{{ route('getDeleteRequest', ':id') }}";
    	url = url.replace(':id', id);
    	document.location.href=url;
    }
    
  • How to call Laravel route in jQuery?

    If you see how to call laravel routes in javascript and jquery then yes there are several ways. We have two examples first is simple route where no any variable pass with route and other is a variable pass with route.

    <script>
    	var url = "{{ route('user.edit', $id) }}";
    	location.href = url;
     
    	//or
    	var id = '12';
    	var url = "{{ route('user.edit', ':id') }}";
    	url = url.replace(':id', id);
    	location.href = url;
     
    </script>
    

Back to code snippet queries related jquery