在Ajax中使用的对象调用PHP文件 [英] Using objects in Ajax calls PHP files
问题描述
我在的index.php
文件实例化的类。但后来我使用jQuery的Ajax调用一些PHP文件,但他们不能使用,我在的index.php
文件中创建我的对象。
I have instantiated a class in my index.php
file. But then I use jQuery Ajax to call some PHP files, but they can't use my object that I created in the index.php
file.
我怎样才能使它发挥作用?因为我不希望创建新的对象,因为我创造了一个拥有所有我想使用的属性值。
How can I make it work? Because I don´t want to create new objects, because the one I created holds all the property values I want to use.
推荐答案
使用会话保存对象下一个页面加载。
Use the session to save the object for the next page load.
// Create a new object
$object = new stdClass();
$object->value = 'something';
$object->other_value = 'something else';
// Start the session
session_start();
// Save the object in the user's session
$_SESSION['object'] = $object;
然后在接下来的页面加载从阿贾克斯
Then in the next page that loads from AJAX
// Start the session saved from last time
session_start();
// Get the object out
$object = $_SESSION['object'];
// Prints "something"
print $object->value;
通过使用PHP的会话,你可以保存多个页面的数据有一定的用户。例如,也许每个用户都有一个包含他们想购买的物品列表的购物车对象。既然你是存储在用户会话中的数据只! - 每个用户都可以具有被保存在每一页上自己的购物车对象
By using the PHP sessions you can save data across many pages for a certain user. For example, maybe each user has a shopping cart object that contains a list of items they want to buy. Since you are storing that data in THAT USERS session only - each user can have their own shopping cart object that is saved on each page!
这篇关于在Ajax中使用的对象调用PHP文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!