0

I have a function which returns a parameter from a table when the user clicks. The data is pulled in via ajax and that is why I am using javascript.

My function is simply:

<script>
 function selectItem(id)
    {
       alert(id);
    }
</script>

What I want is to have a passing to a URL which will pass the id to the lavavel route (rather than the alert which was simply checking the parameter passed), something like

{{ URL('quotations/p_customerselected/{id}') }}

I did try

{{ URL('quotations/p_customerselected/') }} + id;

but threw an error.

1
  • Please provide more of you code Commented May 21, 2017 at 22:05

3 Answers 3

2

Assuming your controller name is QuotationController and controller function name is viewcustomer.

In web.php, give your route a name like

Route::post('/quotations/p_customerselected/{id}', 'QuotationController@viewcustomer')->name('quotations.p_customerselected');

Now in Javascript:

<script>
 function selectItem(cust_id)
 {
   var url = '{{ route("quotations.p_customerselected", ":id") }}';
   url = url.replace(':id', cust_id);
 }
</script>
Sign up to request clarification or add additional context in comments.

Comments

1

I solved it a different (and probably ineligant!) way. On the datatable I added:

{"targets": 0,
                        "data": 'id',
                        render: function(data, type, full, meta) {
                            var id = full['DT_RowId'].substring(4).trim();
                            var url = "{{ URL('quotations/p_customerselected/') }}" +"/" + id;
                            return '<a href="' + url + '"><span style="color: darkblue" title="select">' + full['DT_RowId'].substring(4).trim() +'</span></a>'
                        } 
                    }

It works though!

Comments

0

I don't think that I understand correctly, but if you want pass param to url you can use array to bind them. Something like this

{{ url('quotations/p_customerselected}', [$id]) }}

UPD

if you generate your js code in view, you must insert generated url in quotes.

"{{ url('quotations/p_customerselected}') }}" +  id

3 Comments

The id in question comes from the incoming parameter of a java script function
If I understand correctly, you generate your js code in view. Is't it?
To make it clear, I have a datatable with an ajax load and the incoming id is given from an onClck event so I have a javascript function to catch it. I want now to go to a route with the id passed to the route.

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.