简体   繁体   中英

Pass PHP class instance through AJAX?

I recently wrote a PHP wrapper class for a new API we are using and have been asked to setup a demo which makes use of it. Certain features can be called directly from PHP, however things such as performing actions on button clicks requires I make use of JS/AJAX.

As I already have an instance of the object in my main PHP file, can I pass this as a parameter to JS and then pass it through Ajax to my handler or is it necessary to establish two separate instances?

The closest thing to doing what you want that I can think of would be copying the PHP object to a JavaScript variable via json_encode and then passing that variable back to your PHP code during the AJAX event via the data parameter. PHP code doesn't persist in the way that you seem to be describing - once a page has been requested by a browser, your PHP code for that page is done, there are no variables persisted by the server after that point.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM