0

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?

3
  • 1
    The Ajax PHP file is a completely separate entity, you need to initialize $user there again as you did in the index file Commented Jan 1, 2014 at 4:32
  • Thank you, I thought that might be the case. But I have about 10 pages to load in, so I'd have to do it each time correct? Is this poor practice? Commented Jan 1, 2014 at 4:33
  • It's the only way, unless you find a way to avoid the ajax requests altogether. Just try to limit the amount of stuff you laod at every request to the bare minimum Commented Jan 1, 2014 at 4:35

1 Answer 1

1

You cannot access php variables after the page is executed. So you need to store the data in javascript and send it to the pages using GET/POST where you want to use them.

Add the below code after "print_r($user);"

<script>
 var users_list = <?php echo json_encode($user); ?>;
</script>

Change your javascript code as below

$(document).on('click', '#manageUsers', function(){
 $("#content").load('pages/manageUsers.php', {"users": users_list});
 return false;
});

Change manageUsers.php code to

<h3>List Users</h3>
<?php print_r($_POST['users']); ?>

If you are unaware of usage of JSON .. JSON is a simple way to interchange/maintain/access data ...

$users = array();
$users[1] = array('name' => 'mr.mad', 'age' => '22');
$users[2] = array('name' => 'miss.mad', 'age' => '22');
echo json_encode($users);

the above code will echo

{
 "1": {
  "name": "mr.mad",
  "age": "22"
 }, 
 "2": {
  "name": "miss.mad",
  "age": "22"
 }
}

Make sure even your users array is a structured array like above

Don't use 'click' event directly .. go with "$(document).on('click')" as you are thinking of an ajax application. With this you can use this handler even on newly added dom objects too ..

AJAX is the good way and you are doing it correct with your thought process .. go on ..

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.