3

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.

2
  • This is due to that fact that you can't mix javascript and php in this way. The PHP is going to be run on the server and then sent through to the browser. Commented Nov 22, 2016 at 15:27
  • True, I understood that the Blade Engine pre-processes it and throws the error. I ended up using the URL that calls the route. Thanks. Commented Dec 19, 2016 at 5:25

2 Answers 2

2

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>
Sign up to request clarification or add additional context in comments.

Comments

0

While it's true that you can't mix javascript and php in this way. You can achieve a similar result by doing:

<script>
    ...
    var recordId= 1;
    var formAction = '{{ route('storeRecord', ['id' => ':id']) }}';
    formAction = formAction.replace(':id', recordId);
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.