1

I am trying to send two variables in route through java script function. Javascript function:

function bookmark()
{       
     var url = '{{ route("save_bookmark",":b_id",":p_no") }}';       
      url = url.replace(':b_id', book);
      url = url.replace(':p_no', count);
     document.location.href = url;

} 

book and count variables have been defined up in script.

When I pass only one variable through this code, it works fine but when I try to pass another variable it gives me error..

Missing required parameters for [Route: save_bookmark] [URI:save_bookmark/{b_id}/{p_no}].

My route:

Route::get("save_bookmark/{b_id}/{p_no}",'BookmarkController@create')->name('save_bookmark');

Do anyone know what's happening?

4
  • your blade template is going to render and execute the route before your javascript even replaces the variables. Commented Jun 3, 2019 at 15:22
  • So what is proper way then? Commented Jun 3, 2019 at 15:23
  • 2
    route('save_bookmark', ['b_id' => ':b_id', 'p_no' => ':p_no']) Source Commented Jun 3, 2019 at 15:25
  • This worked!! Thankyou Commented Jun 3, 2019 at 15:30

3 Answers 3

3

you can still do it this way!

javascript

function bookmark()
{       
     var url = '{{ route("save_bookmark",":b_id",":p_no") }}';       
      url1 = url.replace(':b_id', book);
      url2 = url.replace(':p_no', count);
     //document.location.href = url;
     document.href="https://example.com/save_bookmark/"+url1 +"/"+url2;
      or
     location.href="https://example.com/save_bookmark/"+url1 +"/"+url2;

} 
Sign up to request clarification or add additional context in comments.

Comments

0

Looks Good: try it =>

let val1= "1122";
let val2= "a1b2c3";
    var processUrl = '{{ url("/path/:param1/:param2") }}';
        processUrl = processUrl.replace(':param1', val1);
        processUrl = processUrl.replace(':param2', val2);
    document.location.href = processUrl;

Comments

0

Works for me

function bookmark()
{
  var book = 1; var count = 2;
  var url = '{{ route("save_bookmark",[":b_id",":p_no"]) }}';
  url = url.replace(':b_id', book);
  url = url.replace(':p_no', count);
  console.log(url);
}

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.