Laravel - Call route with required parameter using Javascript


I have a named route with a required parameter like this:

../storeRecord/{id}

I want to pass a value to ID from the results of a search. It's a form which has a search bar to assign new data to the search results. I first search for an object and then use its ID to identify the object to which the new data must be saved.

I tried to first create a string using Javascript string concatenation, and then set that as a form action. My inline JS looks like this.

<script>
    ...
    var $rVal = String($('#result').attr('value'));
    var $formAction = "{{route('storeRecord',[" + $rVal + "])}}";
</script>

This throws an "undefined variable : rVal" error. The error seems to be due to the double curly braces which have not been terminated within the first part of the string.

I need to know how to use the search result for the required parameter.

you can't mix javascript with php, try to call your backend like this :

 <script>
    ...
    var $rVal = String($('#result').attr('value'));
    var $formAction = "{{ url('storeRecord') }}/" + $rVal;
 </script>