-1

Hi stackoverflow people,

I'm making a variable code where you can easily put another file-code in between e.g. a div. I'm wondering if you can include a PHP-file via JavaScript, with JS variables. I can do it via PHP but can I include a file with JS?

HTML:

<div id="someId">THE INCLUDE HERE</div>

JS:

var tag_id = 'someId'; //comes form DB
var url = 'data/includes/code.php'; // comes from DB

$('#' + tag_id).html('<?php include("' + url + '"); ?>');

As you can see, tag_id is the id of a object and the url is not static but comes from the DB.


I've read and tried those (but didn't work..):

Maybe I have to think out of my box, can you guys help me?

(Sorry for bad English...)

Community
  • 1
  • 1
Dirk Jan
  • 2,355
  • 3
  • 20
  • 38

1 Answers1

1

No you cant include a php page through a JS variable, however you could use AJAX to retrieve the HTML contents and place it in a DOM like so:

$.ajax({
    type: "GET",
    url : "data/includes/code.php",
    success: function(result) {
        $('#' + tag_id).html(result);
    }
});

This can be simplified to:

$('#' + tag_id).load(url);
Daniel McAssey
  • 436
  • 5
  • 17