I am newbie in javascript. I want to load my website main template file(header,footer,sidebar) using ajax. My layout file is given below.
layout.php
<?php
$app = new App;
$ret = $app->header();
$ret .= $app->sidebar();
$ret .= $app->footer();
echo json_encode($ret);
?>
Now I want to load my layout file using ajax. As there is no div initially. How can I load this.My js file is given below.
$.ajax({
url: 'layout.php',
dataType: 'json',
}).done(function(resp){
//here is problem
//as initially no div is present
//how can I load it with jquery
});
You can do this, looks fine. Don't forget the asynchronous of queries. So your code will:
$.ajax({
url: 'layout.php',
dataType: 'json',
}).done(function(resp){
$('#content').html(resp.layout);
});
<?php
$app = new App;
$ret = $app->header();
$ret .= $app->sidebar();
$ret .= $app->footer();
echo json_encode({layout: $ret});
?>