I have a script which loads several pages (views/home.php, views/about.php, views/contact.php etc):
$(document).ready(function() {
$('#content').load('views/home.php');
$('ul#nav li a').click(function(e) {
var pageq = $(this).attr('href');
$('#content').load('views/' + pageq + '.php');
document.title = $(this).attr('rel');
//to get the ajax content and display in div with id 'content'
$.ajax({
url:'views/'+pageq+'.php',
success: function(data){
$('#content').html(data);
}
});
//to change the browser URL to 'pageq'
if(pageq!=window.location){
window.history.pushState({path:pageq},'',pageq);
}
return false;
});
});
But it works only with html, for ex. <h1>Home</h1>
.
Now I would add some variables to this pages. For example in setup.php there's a variable $page. How to display <h1>$page['title']</h1>
?
I tried with include('config/setup.php');
but it doesn't work.
I also tried this in my .js file: $.get('config/setup.php', { x : y });
, but I don't know how to pass this variables and what is the best approach.
EDIT - navigation:
<?php
try{
$pdo = new PDO("mysql:host=localhost;dbname=mysite", 'root','root');
}
catch(PDOException $e) {
echo $e->getMessage();
}
$sql = "SELECT * FROM posts ORDER BY id";
$stmt = $pdo->prepare($sql);
$stmt->execute();
?>
<ul class="nav navbar-nav" id="nav">
<?php
while ($row = $stmt->fetch(PDO::FETCH_OBJ)) {
$sub_sql = "SELECT * FROM submenu WHERE cat_id=:id";
$sub_stmt = $pdo->prepare($sub_sql);
$sub_stmt->bindParam(':id',$row->id,PDO::PARAM_INT);
$sub_stmt->execute();
?>
<li class="dropdown <?php selected($path['call_parts'][0], $row->slug, 'active') ?>"><a href="<?php echo $row->slug; ?>" rel="<?php echo $row->title; ?>"><?php echo $row->title; ?></a>
<?php
if($sub_stmt->rowCount()) {
?>
<ul class="dropdown-menu" role="menu">
<?php
while ($sub_row = $sub_stmt->fetch(PDO::FETCH_OBJ)) {
?>
<li><a href="<?php echo $sub_row->href; ?>"><?php echo $sub_row->sub_name; ?></a></li>
<?php
}
?>
</ul>
<?php
}
?>
</li>
<?php
}
?>
</ul>