Call an instantiated class's function from javascript

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
imaxeman69
Forum Newbie
Posts: 4
Joined: Tue Jan 26, 2016 8:31 am

Call an instantiated class's function from javascript

Post by imaxeman69 »

First some background and please let me know if what I'm doing is not on the right track.

I have a php page that has a form that accepts some input.

I also have a PHP class that basically does a bunch of queries that I instantiate at the top of the form.

When an event happens, say an input box's onChange event, I'd like to call a function in the instance of my PHP class that, say returns an array, that I can manipulate with Javascript.

I realize that this is going to be some sort of AJAX call, but I can't seem to figure out how to access the instance of my class's functions.

Hope this makes sense.

Thanks in advance.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Call an instantiated class's function from javascript

Post by Celauran »

Sounds like you want to expose a public API, call its endpoints over AJAX, and instantiate your object based on parameters sent to the endpoint.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Call an instantiated class's function from javascript

Post by Christopher »

imaxeman69 wrote:I realize that this is going to be some sort of AJAX call, but I can't seem to figure out how to access the instance of my class's functions.
Well ... whatever PHP script is being called will instantiate you class, call the requested method (with any parameters passed) which will return an array, convert the array to JSON and finally echo the JSON as the response. The Ajax call receives the JSON, converts it into an array and accesses the data.

The PHP script called depends on your architecture. It could just be mypage.php, or if you are using a MVC framework it would be an Action method in a Controller dispatched by a Front Controller.
(#10850)
Post Reply