Reputation: 381
I have a task: make tree class, and ALL methods like (add node, delete node, copy, move) must be called by ajax.
In index.php i wrote (tree must be displayed by loading file):
$tree = new Tree($data);
Tree was successfully created.
So, the question is: how can i work with object by ajax (add_node.php, delete_node.php, ...), that was created in index file?
The simpliest way in add_node.php file wrote again:
$tree = new Tree($data);
, but i dont think that it's good idea to produce another exactly the same object.
Other idea is to send a node php variable in js functions, but i dont know how to do this. I think that it is impossible.
Anyone have any idea? Thanks for any help
Upvotes: 1
Views: 310
Reputation: 167172
Simple answer, you cannot. But you can provide a mechanism to pass the data in the server side itself using a key.
The Tree
object is a server side code. So I guess it would be wise to use sessions to store and retrieve data. Use the client side AJAX to have the identifiers for the session variables in the session and pass the session variables instead of using the real object.
<?php
session_start();
$_SESSION["myData"] = new Tree($data);
And now in the client side, pass the myData
instead of the $_SESSION["myData"]
. Server side code and client side code must have a better separation. You cannot pass objects through client side HTTP Requests.
The URL might be like: add_node.php?id=myData
and you will access it this way:
<?php
session_start();
$tree = $_SESSION[$_GET["id"]];
Here you are accessing the original object and not creating an entirely new object.
The other way is to use serialisation. But in your case, I am not sure if the serialisation can deserialise into the same object as before.
Upvotes: 1