1

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

1 Answer 1

1

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.

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.