1

I am trying to load a page using jquery load function in laravel. My code is,

$(document).ready(function() {
    $('.btn').on('click', function(){
        id = this.id;
        $('#div-details').load('{{URL::to("users/details/'+id+'/view")}}');
    });
});

When I try this code I am getting an error,

syntax error, unexpected ''+id+'' (T_CONSTANT_ENCAPSED_STRING)

When I try the load function without id variable works fine. Like,

 $('#div-details').load('{{URL::to("users/details/55/view")}}');

How can I load the id properly ?

5
  • Can you try id = $(this).attr('id'); Commented Sep 10, 2015 at 8:58
  • Pls provide us with button html markup too. Commented Sep 10, 2015 at 9:13
  • Hit static url not the expression you wrote like $('#div-details').load('users/details/55/view');. can you post the result of it. Commented Sep 10, 2015 at 9:13
  • @jai It correctly loaded the view when using static url Commented Sep 10, 2015 at 9:15
  • @VinodVT then you can use it: $('#div-details').load('users/details/'+id+'/view'); Commented Sep 10, 2015 at 9:18

4 Answers 4

2

Just pass it as a legal path, check the code below

$(document).ready(function() {
$('.btn').on('click', function(){
    var id = $(this).attr('id');
    $('#div-details').load('users/details/'+id+'/view');
});});
Sign up to request clarification or add additional context in comments.

Comments

2

You are mixing worlds.

Take a look, you defined the id variable in JavaScript:

id = this.id;

But then later you use it in PHP

{{URL::to("users/details/'+id+'/view")}}

You cannot use a JavaScript variable in PHP. JavaScript runs in the browser and PHP runs on the server. What you have to do is use PHP to generate the javaScript code that initialises the variable.

$(document).ready(function() {
    $('.btn').on('click', function(){
        id = '{{$i}}';
        $('#div-details').load('{{URL::to("users/details/'+id+'/view")}}');
    });
});

That sample code assumes the $id variable is passed to the view via your controller.

Comments

0

It will work perfectly when I use like this,

$(document).ready(function() {
$('.btn').on('click', function(){
    var id = $(this).attr('id');
    $('#div-details').load('{{URL::to("users/details/")}}/'+id+'/view');  // Load like this
});});

Comments

0

Just do it simply :

$(document).ready(function() {
  $('.btn').on('click', function(){
      var id = this.id;
      var url = "users/details/"+id+"/view";
      $('#div-details').load(url);
  });
});
// Another Way 
$(document).on('click','.btn', function(){
      var id = this.id;
      var url = "users/details/"+id+"/view";
      $('#div-details').load(url);
  });
});

Keep coding, and keep sharing your resource.

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.