Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 392
merovingienne Avatar asked Jun 01 '26 08:06

merovingienne


1 Answers

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>
like image 145
yoeunes Avatar answered Jun 02 '26 22:06

yoeunes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!