I have an issue with PHP. I have an index
file like this:
<?php session_start(); ?>
<!doctype html>
<html lang="en">
<head>
...
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>
<?php
include $_SERVER['DOCUMENT_ROOT'] . '/config.php';
function __autoload($class_name) {
include $_SERVER['DOCUMENT_ROOT'] . '/classes/' . $class_name . '.php';
}
include '../shared/navigation-bar.php';
$user = new Users($db);
print_r($user);
//this prints fyi - so all is well here
?>
<div class="fill grey-bg">
<?php include(dirname(__FILE__) . '/sidebar.php');?>
<div id="content" class="col-md-10 white-bg">
</div>
</div>
<script src="../shared/js/bootstrap.js"></script>
<script src="./js/app.js"></script>
</body>
</html>
All is well here. Now I have a sidebar included, which does this:
<table class="table">
<tr>
<td>
<a href="#" id="manageUsers">Manage Users</a>
</td>
</tr>
</table>
Which when clicked uses an ajax call to load a new page:
$("#manageUsers").click(function(){
$("#content").load('pages/manageUsers.php');
});
Ok so this loads the new page - manageUsers.php
<h3>List Users</h3>
<?php print_r( $user ) ;?>
Now, problem here as you can probably guess is that the $user
doesn't print out in the loaded in page. I'm guessing it's a scope issue. I define it first time when the index page loads, and then when I load in the document with the click it can't pass the original $user
variable through to the new page because PHP is serverside and has already loaded?
The question is, what is the way around this - I'm trying to code in an OOP manner, hence loading the classes and creating the object on the index for use in the other pages.
Problem is if I can't do that, I then have to re-include the classes and create a new object on each loaded in page. This seems frightfully inefficient. Is there a way round this?
If I have to use ajax, is there a smart way to do this via ajax? Or should I just drop the OOP plan and write a function list, including it and a new pdo instance in every loaded in page?