4

IS there any way to achieve this:

<script>
    $('#appends').append('<div class="content-section" id="services">' + 
    " @include('visitor.pages.services') " + 
    ' </div>');
</script>

What i want is to append the content of visitor.pages.services to #appends div

visitor.pages.services is not a php url or something. It is just some lines of html where i am including in my main page.

this is the file

<div class="container">
    <div class="row">
        <div class="heading-section col-md-12 text-center">
            <h2>Our Services</h2>
            <p>We design mobile-first websites for you</p>
        </div> <!-- /.heading-section -->
    </div> <!-- /.row -->
    <div class="row">
        @foreach($data['services'] as $service)
            ... bla bla
        @endforeach
    </div> <!-- /.row -->
</div> <!-- /.container -->
Mohit Tanwani
  • 6,608
  • 2
  • 14
  • 32
Iraklis
  • 208
  • 1
  • 5
  • 16
  • You need to include more info about 'visitor.pages.services' if Loading..'s answer didn't fix your problem. – Andrew Oct 24 '16 at 13:44
  • 1
    @Andrew I fixed the problem by creating a url for this partial. btw i am in Laravel. Now i have another problem in passing $data['services']. If you have some idea please share :) – Iraklis Oct 24 '16 at 13:51
  • Actually i fixed it all. Thanks a lot :))) – Iraklis Oct 24 '16 at 13:53

1 Answers1

7

As your question says, you want to get the content of the file and append it to the specific element.

Use $.get()

$.get("visitor.pages.service.html", function(data){
    $('#appends').append(data);
});

OR .load()

$('#appends').append( $('<div>').load('visitor.pages.service.html'));

OR .ajax()

$.ajax({
    url: "your.html",
    success: function (data) { $('#appends').append(data); },
    dataType: 'html'
});
Mohit Tanwani
  • 6,608
  • 2
  • 14
  • 32
  • Hey Loadin.. thanks for helping. It is just that my data is not a url content. Is justs lins of php file that i am including in my main page. What i want is to append those php lines when i want to – Iraklis Oct 24 '16 at 13:38
  • 1
    @Iraklis if you want to get the data from server (php) in client (jquery), you need to use `$.ajax()` to get the data from PHP. – Mohit Tanwani Oct 24 '16 at 13:39
  • If i do what you are saying correctly, i will get all the data of my main page and not only the partial data that i want. If i 'get' the data url i wil be forced to get all the data of main page and not only the services. Am I right? – Iraklis Oct 24 '16 at 13:43
  • I cannot isolate the visitor.pages.services because it is not url it is just a 'partial' of my main view – Iraklis Oct 24 '16 at 13:44
  • I fixed the problem by making a route in Laravel. Now i have problem passing $data['services']. If you have some idea please edit your answer! Thanks! – Iraklis Oct 24 '16 at 13:50
  • 1
    Actually i fixed it all. Thanks a lot :))) – Iraklis Oct 24 '16 at 13:53