dongshukou0240 2014-01-01 04:30
浏览 36
已采纳

使用ajax加载页面时的PHP范围问题

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?

  • 写回答

1条回答 默认 最新

  • douping4436 2014-01-01 11:17
    关注

    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 ..

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用