Using objects in Ajax calls PHP files

I created an instance of the class in my index.php file. But then I use jQuery Ajax to call some PHP files, but they cannot use my object that I created in the index.php file.

How can I make it work? Because I do not want to create new objects, because the one I created contains all the property values ​​that I want to use.

+7
oop ajax php
source share
7 answers

Use a session to save the object to load the next page.

 // 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 session $_SESSION['object'] = $object; 

Then on the next page downloaded from AJAX

 // Start the session saved from last time session_start(); // Get the object out $object = $_SESSION['object']; // Prints "something" print $object->value; 

Using PHP sessions, you can save data on many pages for a specific user. For example, perhaps each user has a shopping cart object that contains a list of items that they want to buy. Since you only save this data in the THAT USERS session, each user can have their own shopping cart object, which is saved on every page!

+16
source share

mm, you must save in the session $_SESSION["someobj"] = $myobj; and make sure that when calling the Ajax PHP file, this includes the class of necessary files, which defines the class $myobj and any object contained in it.

Could you be more specific? I can try.

This is how I create an object, then assign it to a session variable:

 include(whateverfilethathastheclassorincludeit.php) $theObject = new TheObjectClass(); //do something with the object or not $_SESSION['myobject'] = $theObject; 

This is how I refer to the members of an object in my PHP Ajax call file:

 include(whateverfilethathastheclassorincludeit.php) $theObject = $_SESSION['myobject']; //do something with the object 
+3
source share

Another option if you do not want to use sessions is to serialize your object and send it via the $ _POST value in your AJAX call. Not the most elegant way to do this, but a good alternative if you don't want to use sessions.

See Serializing objects in the documentation for more information.

+3
source share

You did not provide the code, but I think you need to make your instance of the object global for other scripts to see it, for example:

 $myobject = new myobject(); 

Now I want to use this object elsewhere, perhaps under some function or class or anywhere where it is not recognized, so I will make it global with a global keyword, and it will also be available there:

 global $myobject; 

Once you have the object, you can put it into the session and then use it in the Ajax script file.

+1
source share

If you do not want to move your object that is in your index.php, ask ajax to make an index.php request, but add additional parameters (post / get) that let your index.php know the process is like an ajax request and not return your standard output of html web page.

+1
source share

As others have shown, $_SESSION is the standard way to do this, in fact, this was one of the reasons that the sessions were invented for the solution. Other options, that is, object serialization, rely on the client side to store the object, and then return it unchanged. Depending on the data in the object, this is not a good solution, since a) the object may contain information that should not be accessible on the client side for security reasons, and b) you will need to check the object after receiving it.

However, if you still want to use the client-side object, then JSON is an option for serializing the object data, see JSON Functions in PHP .

0
source share

Based on most of the answers here, referring to storing the object in $ _SESSION, is it more efficient to store only the individual properties that should be available in AJAX, unlike the whole object, or does it not matter?

eg.

 $_SESSION['object'] = $object; 

vs

 $_SESSION['property1'] = $object->property1; $_SESSION['property2'] = $object->property2; 

I know that the OP is asking about access to the whole object, but I think my question relates to the fact that it is just a matter of accessing certain properties of the object and not requiring access to the class methods to change the object once in AJAX.

0
source share

All Articles