skip to Main Content

I have this script at my Blade:

<script>
function getComboA(selectObject) {
    var value = selectObject.value;  
    window.location.href = {!! json_encode(route('changeRequestStatusNhd', value)) !!}
}
</script>

Basically, the variable value holds the value of select option input and I need to pass this variable to the route changeRequestStatusNhd as its a parameter.

But the way I pass it in route('changeRequestStatusNhd', value) is wrong (Use of undefined constant value - assumed 'value' (this will throw an Error in a future version of PHP))!

But I don’t know what is the proper way of passing this js variable to laravel route…

So if you know, please let me know..

Thanks

2

Answers


  1. There are two ways.

    way 1:

    In blade files, inside the script tag used like this

     var test = "{!! $test_varibale !!}";
    

    way 2:

    You can declare value in blade file like way 1 type. then you can use the variable in the js file

    Login or Signup to reply.
  2. <script>
    function getComboA(selectObject) {
        var value = selectObject.value;
        var route = "{{ route('changeRequestStatusNhd') }}"; // it will be rendered when page load.
        window.location.href = `${route}/${value}`
    }
    </script>
    

    Laravel’s Variables and function will rendered on page load.
    So it is not possible to bind the Javascript variables on specific events.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search