I have a PHP file, .../datastuff.php On this page a PHP object is instantiated. (The class is written in another file).
public class MyClass{
//....
public function doAfterClick(){
//.....
}
}
So in datastuff.php I do
$MyObject = new MyClass();
//user specific data is added to $MyObject
Now I want to call $MyObject->doAfterClick(), when the user presses a button on datastuff.php
I think AJAX is the general solution for this type of problem. But I am not sure how to do this? If I wanted to call a PHP function written on another page I could use AJAX to POST data to that page. But I want to stay on this page, because MyObject needs to call the method.
If I POST back to datastuff.php won't $MyObject be lost? If I make it a Singleton or Global would this then work? I looked at this, which is a similar problem, ut is not quite what I am looking for: Using JQuery ajax to call a PHP file, process, but stay on same page
$MyObjectvariable won't be the same. It may well be an identical copy to the initial request, but it's not the same object. A singleton or a global won't make any difference - these are only ways of getting the same object within the same request.